C#-split函数的使用

2023-03-03 19:35:27 浏览数 (1)

浏览量 1

split函数的作用是用来分割字符串,通常我们采用的是用什么字符来分割字符串,以达到获取我们想要的字符串,函数的返回值为数组。

常见用法

1.以单个字符分割字符串

代码语言:javascript复制
string str="abc,def,ghi";
string[] strarr=str.split(',');
foreach(string s in strarr) Response.Write(s.ToString()   "<br>");

2.以字符串分割字符串

代码语言:javascript复制
string str="abc||def||ghi";
string[] strarr=str.split(new string[] {"||"},StringSplitOptions.None);
foreach(string s in strarr) Response.Write(s.ToString()   "<br>");
StringSplitOptions是一个枚举类型,它表示你分割之后是否需要返回为空的值。
StringSplitOptions.none表示返回空值。
StringSplitOptions.RemoveEmptyEntries表示去除空值。

3.以多个字符分割字符串

代码语言:javascript复制
string str="abc,def.ghi"; 
string[] strarr=str.Split(new char[2] {',','.'}); 
foreach(string s in strarr) Response.Write(s.ToString()   "<br>");
//关于其6个重载方法
public string[] Split(params char[] separator)
public string[] Split(char[] separator, int count)
public string[] Split(char[] separator, StringSplitOptions options)
public string[] Split(string[] separator, StringSplitOptions options)
public string[] Split(char[] separator, int count, StringSplitOptions options)
public string[] Split(string[] separator, int count, StringSplitOptions options)

有的时候我们常常会写成这种错误的写法:str.split(“||”),我们从split几个重载函数中可以看出,没有这种只有一个参数,并且参数是字符串的函数,所以会报错。

6个重载方法的使用

代码语言:javascript复制
string str="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[] { ',', '.' }, 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()方法 https://msdn.microsoft.com/zh-cn/library/system.string.split(v=vs.110 C#中String.Split方法使用详解 http://blog.csdn.net/riyuedangkong1/article/details/52431209

0 人点赞