split用法,区别一个或多个空格

#region 读取文本文件中的二维数组
        private void 打开文件ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenFileDialog sf = new OpenFileDialog();
            //设置文件读入的类型
            sf.Filter = "txt文件|*.txt|所有文件|*.*";
            //如果用户没有输入扩展名,自动追加后缀
            sf.AddExtension = true;
            sf.Title = "读文件";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                richTextBox2.Text = "";
                StreamReader sr = new StreamReader(sf.FileName);
                string pattern = @"\s*,\s*|\s+";//表达式模式(正则表达式)
                // “\s”表示空格,“\s*”表示0个或多个空格,“\s+”表示1个或多个空格
                string sLine;
                string[] sFields;

                //读取一行文本
                int i = 0;

                while ((sLine = sr.ReadLine()) != null)
                {
                    //将源数据行写入到文本框中
                    richTextBox2.AppendText(sLine + "\n");
                    sFields = Regex.Split(sLine, pattern);
                    //if (sFields[0].Trim() != null)
                    //    continue;
                    n = sFields.Length;

                    for (int j = 0; j < n; j++)
                    {
                        d[i, j] = int.Parse(sFields[j].Trim());
                    }
                    i++;

                }
                p = new int[n];
                V = new int[n + 1];//记录经过的城市
                path = new int[n];
                for (int q = 0; q < n; q++)
                {
                    p[q] = q;
                }


            }


        }
        #endregion

你可能感兴趣的:(C#代码)