下面这一段程序:            string s = " abc   esdf ";
            char[] sz = { ' ', 'e' };
            string snew = s.Trim(sz);
            Console.WriteLine(snew+","+s);
           
运行输出的结果是:
abc   esdf, abc   esdf
为什么没有如我预期的那样去掉空格呢?

解决方案 »

  1.   

    Trim()函数就是删除文字列开头和末尾的中间的不能删除 用 replace吧
    string s = " eabc   esdfe ";            
    char[] sz = { ' ', 'e' };            
    string snew = s.Trim(sz);            
    Console.WriteLine(snew+","+s);
      

  2.   

    like this:
    string s = " abc   esdf ";
                char[] sz = { ' ', 'e' };
                string[] snew = s.Split(sz, StringSplitOptions.RemoveEmptyEntries);
                foreach (string item in snew)
                {
                    Console.WriteLine(item);
                }
      

  3.   

    Trim是去掉开头和结尾的空格
    用正则
      string s = " abc   esdf ";
                char[] sz = { ' ', 'e' };
                string snew = System.Text.RegularExpressions.Regex.Replace(s,@"[\se]*","");
                Console.WriteLine(snew);
      

  4.   

    高手高手!
                string s = " abc   esdf ";
                /*char[] sz = { ' ', 'e' };
                string[] snew = s.Split(sz, StringSplitOptions.RemoveEmptyEntries);
                foreach (string item in snew)
                {
                    Console.WriteLine(item);
                }*/
                Console.WriteLine(System.Text.RegularExpressions.Regex.Replace(s, @"[\se]*", ""));这样就能得到我想要的结果了!