c#中split用法~-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 30002
  • 博文数量: 10
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2020-06-29 11:21
文章分类
文章存档

2020年(10)

我的朋友
相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: 服务器与存储

2020-07-02 15:33:48

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()   "
");
输出结果:
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()   "
"); 
输出结果:
aaa
bbb
ccc
ddd
eee
3、用单个字符来分隔:
string str="aaajbbbjccc";
string[] sarray=str.split('j');
foreach(string i in sarray) response.write(i.tostring()   "
");
输出结果:
aaa
bbb
ccc


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

这是一个具有语法错误的语句,split 的 separator 参数应该是 [] 或 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(',')

阅读(2167) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图