如题,我有下面几行程序:            string s = " abc   esdf ";
            char[] sep = { ' ' };
            string[] Words = s.Split(sep);
            
            foreach (string word in Words)
            {
                Console.WriteLine(word);
            }运行的结果并不是我期待的
abc
esdf
而是:
abc
esdfPress any key to continue . . .
中间多出了好几个空行,开头也多出了空行。难道Split会把delimiter(这里是' ')替换成回车?

解决方案 »

  1.   

    string[] Words = s.Split(sep, StringSplitOptions.RemoveEmptyEntries);
      

  2.   

    楼主可以查查String.Split的用法
    Split(Char[])  返回的字符串数组包含此实例中的子字符串(由指定 Unicode 字符数组的元素分隔)。
    意思就是每一个Char[]为分隔符,符合条件就分割字符串。
    string s = " abc   esdf ";
    char[] sep = { ' ' };
    string[] Words = s.Split(sep);
    这里的意思就是字符串中,每当有一个sep字符串数组的话,就把当前字符串分成两段。