C#中Split用法~
2021-07-14 18:14
标签:params param express word ignore 不能 语句 ext write 这是一个具有语法错误的语句,Split 的 separator 参数应该是 char[] 或 string[],不应是字符串。正确的示例: C#中Split用法~ 标签:params param express word ignore 不能 语句 ext write 原文地址:https://www.cnblogs.com/qiu18359243869/p/9534262.html
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";
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
char[] separator = { ‘o‘ };
string[] arr = str.Split(separator);
String.Split 方法有6个重载函数:
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"}
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.None);//返回:{"1","2","3","","4"} 保留空元素
4. public string[] Split(string[] separator, StringSplitOptions options)
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[] { ‘,‘, ‘.‘ }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6. public string[] Split(string[] separator, int count, StringSplitOptions options)
string[] split = words.Split(new string[] { ",", "." }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
上一篇:c# 获取项目的根目录