c中split的用法

 我来答
华源网络
2022-06-26 · TA获得超过5599个赞
知道小有建树答主
回答量:2486
采纳率:100%
帮助的人:148万
展开全部

  c中split的用法你知道吗?下面我就跟你们详细介绍下c中split的用法,希望对你们有用。

  c中split的用法如下:

  1、用字符串分隔:

  using System.Text.RegularExpressions;

  string str="aaajsbbbjsccc";

  string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);

  foreach (string i in sArray) Response.Write(i.ToString() + "<br>");

  输出结果:

  aaa

  bbb

  ccc

  2、用多个字符来分隔:

  string str="aaajbbbscccjdddseee";

  string[] sArray=str.Split(new char[2] {'j','s'});

  foreach(string i in sArray) Response.Write(i.ToString() + "<br>");

  输出结果:

  aaa

  bbb

  ccc

  ddd

  eee

  3、用单个字符来分隔:

  string str="aaajbbbjccc";

  string[] sArray=str.Split('j');

  foreach(string i in sArray) Response.Write(i.ToString() + "<br>");

  输出结果:

  aaa

  bbb

  ccc

  ////////////////////////////////////////////////

  string[] arr = str.Split("o");

  这是一个具有语法错误的语句,Split 的 separator 参数应该是 char[] 或 string[],不应是字符串。正确的示例:

  string str = "technology";

  char[] separator = { 'o' };

  string[] arr = str.Split(separator);

  ////////////////////////////////////////////////////

  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(',')

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式