关闭

C#的String.Split方法

95人阅读 评论(0) 收藏 举报

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, StringSplitOptionsoptions)
6) public string[] Split(string[] separator, int count, StringSplitOptionsoptions)


下边我们通过一些实例来说明下怎么使用(以下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"}保留空元素


需要注意的是没有重载函数publicstring[] Split(string[] separator),所以我们不能像VB.NET那样使用words.Split(","),而只能使用words.Split(',')!很多人都很奇怪为什么把双引号改为单引号就可以了?看了上边的重载函数该知道答案了吧^_^

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14432次
    • 积分:340
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:11篇
    • 译文:1篇
    • 评论:0条
    文章存档