本帖最后由 刘赛 于 2013-4-26 16:32 编辑
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"} 保留空元素
复制代码 |
|