C# 根据空格数截取

      #region --根据空格数截取

                    string temp = ""; int k = 0; int jc = 0;
                    //循环截取
                    int[] rules1 = { 7, 6 };
                    while (!sr.EndOfStream)
                    {
                        for (int i = 0; i < strTest.Length - 1; i++)
                        {
                            string s = strTest.Substring(i, 1).Trim(); //截取当前字符
                            string s2 = strTest.Substring(i + 1, 1).Trim();//截取后一个字符
                            temp = temp + s;

                            //判断当前字符为最后一个字符
                            if (string.IsNullOrEmpty(s)) //判断当前字符串为空时候
                            {
                                k++; //记录空格数
                                if (jc >= rules1.Length) //字符串长度过长时候,终止
                                {
                                    break;
                                }
                                if (k == rules1[jc])
                                {
                                    jc++;
                                    result += temp + "\t";
                                    temp = string.Empty;
                                    k = 0;
                                }
                            }
                            if (i == strTest.Length - 2)
                            {
                                result += temp + "\r\n"; //最后一个
                                temp = string.Empty;
                                jc = 0;
                                break;
                            }

                        }
                        strTest = sr.ReadLine();
                        if (strTest.Contains("日期"))
                        {
                            break;
                        }

                    }
                    #endregion

 

posted @ 2018-08-03 09:42  enych  阅读(876)  评论(0编辑  收藏  举报