加载中…
个人资料
小宇
小宇
  • 博客等级:
  • 博客积分:0
  • 博客访问:101,723
  • 关注人气:20
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

C#中String.Split()拆分字符串的用法(转)

(2014-07-14 15:50:03)
标签:

佛学

分类: 软件技术

                                           C#中String.Split()拆分字符串的用法(转)

String.Split方法有6个重载函数:
1) public string[] Split(params char[] separator)
2) public string[] Split(char[] separator, int count)
3) public string[] Split(char[] separator, StringSplitOptions options)
4) public string[] Split(string[] separator, StringSplitOptions options)
5) public string[] Split(char[] separator, int count, StringSplitOptions options)
6) public string[] Split(string[] separator, int count, StringSplitOptions options)

下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):

1. public string[] Split(params char[] separator)
string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}

2. public string[] Split(char[] separator, int count)
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}

3. public string[] Split(char[] separator, StringSplitOptions options)
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

4. public string[] Split(string[] separator, StringSplitOptions options)
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

5. public string[] Split(char[] separator, int count, StringSplitOptions options)
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

6. public string[] Split(string[] separator, int count, StringSplitOptions options)
string[] split = words.Split(new string[] { ",", "." }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

需 要注意的是没有重载函数public string[] Split(string[] separator),所以我们不能像VB.NET那样使用words.Split(","),而只能使用words.Split(',')!很多人都很 奇怪为什么把双引号改为单引号就可以了?看了上边的重载函数该知道答案了吧^_^
 
=======================================================================================================
 
对 于Split中我用得最多的就是string[] str=string.Split(new char[]{''}),由于以前对于由字符来分隔生成数组能满足需求,对其他的也没有怎么去认识,可今天出现了由字符解决不了的问题了,如由字符串或特 殊字符、制表符等分隔需求时,还是把一些要用的记下来吧
Split的几种重载方式:
1) public string[] string.Split(params char[] separator)
2) public string[] string.Split(char[] separator, int count)
3) public string[] string.Split(char[] separator, StringSplitOptions options)
4) public string[] string.Split(string[] separator, StringSplitOptions options)
5) public string[] string.Split(char[] separator, int count, StringSplitOptions options)
6) public string[] string.Split(string[] separator, int count, StringSplitOptions options)
例:string str_exam="a,b.cfgd\t1,,2";
Split的几种分隔方式:
1.用字符来分隔生成数组
1.1用单个字符来分隔
string[] str=str_exam.Split('.');     //返回:{"a,b","cfgd\t1,,2"}
 
string[] str=str_exam.Split(new char[]{'.'});   //返回:{"a,b","cfgd\t1,,2"}
 
string.Split(char[] param,int count)     //param为用于分隔的字符,count为返回的串的最大数量
如:string[] str=str_exam.Split(new char[]{','},8);   //返回:{"a","b.cfgd\t1","","2"}
string[] str=str_exam.Split(new char[]{','},2);       //返回:{"a","cfgd\t1,,2"}
 
string.Split(char[] param,int count,StringSplitOptions options)    //param为用于分隔的字符,count为返回的串的最大数量,options指定是否要返回空数组元素
如:string[] str=str_exam.Split(new char[]{','},StringSplitOptions.None);  //返回:{"a","b.cfgd\t1","","2"}
string[] str=str_exam.Split(new char[]{','},8,StringSplitOptions.None);  //返回:{"a","b.cfgd\t1","","2"}
string[] str=str_exam.Split(new char[]{','},8,StringSplitOptions.RemoveEmptyEntries);  //返回:{"a","b.cfgd\t1","2"}
1.2用多个字符来分隔
string[] str=str_exam.Split(new char[2]{',','.'});   //返回:{"a","b","cfgd\t1","","2"}
string[] str=str_exam.Split(new char[]{',','.'});    //返回:{"a","b","cfgd\t1","","2"}
多字符分隔的的count和options的用法与单字符的一致。
 
 
2.用字符串来分隔生成数组
2.1用单个字符串来分隔
string[] str=str_exam.Split(new string[]{","});    //返回:{"a","b.cfgd\t1","","2"}
string[] str=str_exam.Split(new string[]{","},2);   //返回:{"a","b.cfgd\t1,,2"}
string[] str=str_exam.Split(new string[]{","},6,StringSplitOptions.RemoveEmptyEntries);   //返回:{"a","b.cfgd\t1","2"}
string[] str=str_exam.Split(new string[]{"fg"});   //返回:{"a,bc","d\t1,,2"}
2.2用多个字符串来分隔
string[] str=str_exam.Split(new string[]{",","fg"});   //返回:{"a","b.c","d\t1","","2"};
string[] str=str_exam.Split(new string[]{",",".","fg","\t"},6);     //返回:{"a","b","c","d","1",",2"};
string[] str=str_exam.Split(new string[]{",","\t"},6,StringSplitOptions.RemoveEmptyEntries);  //返回:{"a","b.cfgd","1","2"};
注意:字符串分隔的count与options和字符分隔的用法一致

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有