C# 如何用多个字符串来切分字符串并去除空格

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;  
  6.   
  7. namespace Study  
  8. {  
  9.     public static class Program3  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Proccess(@"13212345671,13312345672,  13412345674  
  14.             13212345674,");  
  15.             Proccess("");  
  16.             Proccess(null);  
  17.             Console.Read();  
  18.         }  
  19.   
  20.         public static void Proccess(string str)   
  21.         {  
  22.             //双问号保证 null 值不会异常  
  23.             //StringSplitOptions.RemoveEmptyEntries 会移除 string.Empty 空串, 但对于空格无能为力  
  24.             //' '将按空格来切分,所以不再有空格出现  
  25.             string[] arr = (str ?? string.Empty).Split(new char[] { ',', '\t', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries);  
  26.             Console.WriteLine("本次切分后数组的长度为:{0}", arr.Length);  
  27.             int i = 1;  
  28.             foreach (string s in arr)  
  29.             {  
  30.                 Console.WriteLine("{0}:{1}", (i++).ToString(), s);  
  31.             }  
  32.         }  
  33.     }  
  34. }  
 
posted @ 2015-06-28 12:03  大林just  阅读(2937)  评论(0编辑  收藏  举报