码迷,mamicode.com
首页 > 其他好文 > 详细

String.Split()函数

时间:2016-06-21 07:43:02      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

String.Split()

用法为:       

   string[] ss1 = s.Split(new[] {"|:|"}, StringSplitOptions.None);
            //ss1[0]: A|B
            //ss1[1]: C:D
后一个参数是标志是否自动remove empty的数据例如: "A|B|:|C:D|:|" 做分割时会返回三个数组该数组的最后一个元素是空""如果我们想过滤掉这些空的元素可以使用参数: StringSplitOptions.RemoveEmptyEntries string[] ss1s.Split(new[] {"|:|"}, StringSplitOptions.RemoveEmptyEntries); 
例子:         
            s = "A|B|:|C:D|:|";
            string[] ss1 = s.Split(new[] {"|:|"}, StringSplitOptions.RemoveEmptyEntries);
            //ss1[0]: "A|B"
            //ss1[1]: "C:D"

            ss1 = s.Split(new[] { "|:|" }, StringSplitOptions.None);
            //ss1[0]: "A|B"
            //ss1[1]: "C:D"
            //ss1[2]: ""

首先我们看下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"} 保留空元素

String.Split()函数

标签:

原文地址:http://blog.csdn.net/heyuchang666/article/details/51699840

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!