楼上的方法是不行的。
根据MSDN, 实际上 Split() 等价于 Split(' ','\t','\n',...)
其中的 ... 表示其它Unicode空白字符。
可以用下面的代码验证:
class Test
{
  static void Main()
  {
    foreach (string s in " a  bc d ".Split(' '))
      System.Console.Write("[{0}]", s);
  }
}
结果是: [][a][][bc][d][]
但我希望是: [a][bc][d]

解决方案 »

  1.   

    To: ismezy2002(扬)
    自已写是不难,但这是很常用的功能,我想.NET framework应该支持的才对。
    如果用自己写的,每次使用时都要加入自己写的函数,很麻烦。
      

  2.   

    using System.Text.RegularExpressions ;
    class Test
    {
      static void Main()
      {
       string s=" a  bc d ";
       Regex rule=new Regex ("[a-z][^ ]*");
    MatchCollection mc=rule.Matches (s);
        foreach (Match m in mc)
        {
          System.Console.Write("[{0}]", m.Value);
        }
      }
    }
      

  3.   

    microblue(microblue) 人工控制一下算了,
    foreach (string s in " a  bc d ".Split(' ')) if(s!="")
    System.Console.Write("[{0}]", s);另:不要称呼别人为"楼上的",那样很不礼貌.
      

  4.   

    要不这样吧。string s = " a  bc d ";
    s.Replace(" ",",")string [] ss = s.Split(new char[]{','});
      

  5.   

    cwbboy(好想失恋)的方法和我的差不多,
    不过,有一个失误(第二句)我的如下:
    string s = " a  bc d ";
    s=s.Trim().Replace(" ","\n");
    char[] chr=null;string [] ss = s.Split(n);不过,这样也是不行的,因为a和bc之间有两个空格
      

  6.   

    To: cwbboy(好想失恋)
    s.Replace(" ",",")
    应改为: s = s.Replace(" ",",");
    因为 string 是不可变的。
    此外,这个还是产生:[][a][][bc][d][]
    因为这只是把空格替换为",",然后用","作分隔符而已,没有实质性的改变。
      

  7.   

    sorry

    string [] ss = s.Split(chr);
    这句改一下
      

  8.   

    根据楼主的意思
    好象a和bc之间是没有两个空格的
    是我多虑了
      

  9.   

    To:  yarshray(saga jion(未出山的杨过))
    我是想取得空白分隔的字段的值。如有以下文件in.txt:
    15 1234 7  8
     3    5 8  9
    32  -75 4 12
    程序片段如下:
    StreamReader sr = new StreamReader("in.txt");
    for (;;)
    {
      string s = sr.ReadLine();
      if (s == null) break;
      string [] ss = s.Split();
      int [] a = new int [ss.Length];
      for (int i = 0; i < a.Length; i++)
      {
        a[i] = int.Parse(ss[i]);
      }
      // 处理 a[i] ...
    }
    sr.Close();
    in.txt 文件中有四栏,我希望处理这四栏的值。
    但 Split() 会返回多余的空字符串,不能满足要求。
      

  10.   

    To:  HellMaster(李晋)
    a 和 bc 之间是有两个空格的。
    而且可能是多个空格,或者是'\t'之类。
    我的意思是说分割空白定界的字符串。多个空白字符当作一个看。
      

  11.   

    这个很简单呀。。
    看这个很快就可以实现了
    string s = " a  bc d ".Trim();
    string[] sr = new string[5];
    int i = 0 ; //数组长度
    while( true )
    {
       int intIndexOf = s.IndexOf( ' ');
       if( intIndexOf > 0 )
      {
         sr[i] = s.Substring( 0 , intIndexOf );
         s = s.Substring( intIndexOf,s.Length - intIndexOf );
         s = s.Trim();
         i++;
       }
     else
     {
    sr[i] = s;
    break;
      }
    }for( int tmp = 0 ; tmp <= i ; tmp ++ )
          System.Console.Write("[{0}]", sr[tmp]);
      

  12.   

    看你的例子排的那么整齐,会不会是用tab分隔的?要不是的话,我觉得简单一点,这样:
    string s = " a  bc d ";
    s=s.Trim().Replace(五个空格,一个空格);
    s=s.Replace(两个空格,一个空格);
    s=s.Replace(两个空格,一个空格);
    //这样最多处理了中间有8个空格的情况,要是空格超过8个,前面再加一句
    //s=s.Trim().Replace(两个空格,一个空格); 可以处理到16个
    //再split s
    string [] ss = s.Split(一个空格);
      

  13.   

    using System.Text.RegularExpressions;

    string sIn  = " a   bc d ";
    sIn = sIn.Trim();
    Regex r = new Regex(@"\s+");
    string[] sArr = r.Split(sIn);
    //sArr = {"a","bc","d"}//对于同样适用string sIn  = " a   bc\td ";
      

  14.   

    多谢TheAres兄,让偶学了正则表达式的用法我的方法如下:
    string s = " a  bc\t d ";
    ArrayList aryLst=new ArrayList();
    s=s.Trim().Replace(" ","\n");
    char[] chr=null;string [] ss = s.Split(chr);for (int i=0;i<ss.Length;i++)
    {
    if (ss[i].Trim()!="")
    aryLst.Add(ss[i]);
    }aryLst就是所求
      

  15.   

    当然
    如果你非要string[]的话
    加上ss=(string[])aryLst.ToArray(typeof(string));
      

  16.   

    // Trim white spaces, commas, and so on.
             string city;
             city = city.Trim(new char[]{' ', ',', ';', '-', ':'});
    你试试这个,简单的办法,可以去除很多不想要的字符,自己改吧。
      

  17.   

    我认为应该将首尾空格去除后再分解,对于中间只有一个空格分隔的用
    string.split(..)方法
    对于可能用多个空格分隔的话:
    System.Text.RegularExpressions.Regex.Split(待分解串,格式串)的方法来分解,不需要创建一个Regex对象。
      

  18.   

    我用这种方法
    class Test
    {
      static void Main()
      {
        foreach (string s in " a  bc d ".Split(' '))
      {  if(s.Trim().lenght!=0)
           System.Console.Write("[{0}]", s);
    }
      }
    }
      

  19.   

    谢谢大家,尤其是 TheAres(班门斧)分割空白定界的字符串。连续的多个空白字符当作一个看,忽略头尾的空白字符。using System.Text.RegularExpressions;string s = " a \t  bc \nd \r ";
    string [] ss = Regex.Split(s.Trim(), @"\s+");
    // ss == {"a","bc","d"}