首页 > Windows开发 > 详细

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

时间:2015-06-28 12:30:03      阅读:256      评论:0      收藏:0      [点我收藏+]
  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. }  
技术分享
 

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

原文:http://www.cnblogs.com/just09161018/p/4605333.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!