A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘赛 中级黑马   /  2013-4-26 16:21  /  2343 人查看  /  7 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 刘赛 于 2013-4-26 16:32 编辑

C#的String.Split
方法String.Split 方法有6个重载函数:
程序代码
  1. 1) public string[] Split(params char[] separator)

  2. 2) public string[] Split(char[] separator, int count)

  3. 3) public string[] Split(char[] separator, StringSplitOptions options)

  4. 4) public string[] Split(string[] separator, StringSplitOptions options)

  5. 5) public string[] Split(char[] separator, int count, StringSplitOptions options)

  6. 6) public string[] Split(string[] separator, int count, StringSplitOptions options)
复制代码
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
1.
  1. public string[] Split(params char[] separator)
复制代码
程序代码
  1. string[] split = words.Split(new Char[] { ',' });//返回:{"1","2.3","","4"}
  2. string[] split = words.Split(new Char[] { ',', '.' });//返回:{"1","2","3","","4"}
复制代码
2. public string[] Split(char[] separator, int count)
程序代码
  1. string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
  2. string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
复制代码
3.
  1. public string[] Split(char[] separator, StringSplitOptions options)
复制代码
程序代码
  1. string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
  2. string[] split = words.Split(new Char[] { ',', '.' }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
复制代码
4.
  1. public string[] Split(string[] separator, StringSplitOptions options)
复制代码
程序代码
  1. string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
  2. string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素

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

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

7 个回复

倒序浏览
很详细,支持!
回复 使用道具 举报
学习了!支持!
回复 使用道具 举报
好东西 赞一个
回复 使用道具 举报
很详细哇,支持LZ~~
回复 使用道具 举报
好东西 支持了 以后用得到
回复 使用道具 举报
好东西 支持了 以后用得到
回复 使用道具 举报
很详细啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马