代码如下:
        StreamReader my_stream_reader = new StreamReader("results.txt");
    string getligne = my_stream_reader.ReadLine();
    while (getligne.Contains(" "))
      {          
        getligne  = getligne.Replace(" ", " ");
       }
    string[] sArray = getligne.Split(' ');
    MessageBox.Show(sArray[0], "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);results.txt中是如下的文件:
18 18 18 18 18 18 18 18 18 18 18 18.143 18.146 18.2 18.1788 18.1808 18.179 18.179 18.2 18.1836 18.1854 18.2可是结果还是第一行字符串,根本没有分隔成功。请指教。。

解决方案 »

  1.   

    if (getligne.Contains(" "))
      {   
      getligne = getligne.Replace(" ", " ");
      }这样试试?
      

  2.   

    string[] sArray = getligne.SpSplit(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
      

  3.   

    getligne = getligne.Replace(" ", " ");
    这一句有用吗?空格替换成空格?
      

  4.   


                StreamReader my_stream_reader = new StreamReader(@"E:\test.txt");
                string getligne = my_stream_reader.ReadToEnd().ToString();
                if (getligne.Contains(" "))
                {
                    getligne = getligne.Replace(" ", " ");
                }
                string[] sArray = getligne.Split(' ');            foreach (string s in sArray)
                    Console.WriteLine(s);结果:
    18
    18
    18
    18
    18
    18
    18
    18
    18
    18
    18
    18.143
    18.146
    18.2
    18.1788
    18.1808
    18.179
    18.179
    18.2
    18.1836
    18.1854
    18.2
      

  5.   

    update:           StreamReader my_stream_reader = new StreamReader(@"E:\test.txt");
                string getligne = my_stream_reader.ReadToEnd().ToString();
                if (getligne.Contains(" "))
                {
                    getligne = getligne.Replace(" ", " ");
                }
                string[] sArray = getligne.Split(new char[]{' ','\r','\n'},StringSplitOptions.RemoveEmptyEntries);            foreach (string s in sArray)
                    Console.WriteLine(s);结果:
    18
    18
    18
    18
    18
    18
    18
    18
    18
    18
    18
    18.143
    18.146
    18.2
    18.1788
    18.1808
    18.179
    18.179
    18.2
    18.1836
    18.1854
    18.2
      

  6.   

    情况不妙,改进代码后为:
            StreamReader my_stream_reader = new StreamReader("results.txt");
        string getligne = my_stream_reader.ReadLine().ToString();
        if (getligne.Contains(" "))
          {
             getligne = getligne.Replace(" ", " ");
          }
        string[] sArray = getligne.Split(new char[]{' ','\r','\n'},StringSplitOptions.RemoveEmptyEntries);
        int num = sArray.Count();
        MessageBox.Show(num.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);至少我想看看数组里有几个吧,结果是1。头疼阿,还是不行吧。
      

  7.   

    你把这段代码放在控制台程序里试试: StreamReader my_stream_reader = new StreamReader(@"E:\test.txt");
                string getligne = my_stream_reader.ReadToEnd().ToString();            string[] sArray = getligne.Split(new char[]{' ','\r','\n'},StringSplitOptions.RemoveEmptyEntries);            foreach (string s in sArray)
                    Console.WriteLine(s);
    我在控制台程序输出是正确的
      

  8.   

    List<string> lst=new List<string>(File.ReadAllLines(""))
    foreach(string s in lst)
    {
    string[] arr= s.Split(new string[]{" ","\r\n"},StringSplitOptions.RemoveEmptyEntries);
    }