将字符串str1 = "select * from table "; 进行分割,有两种方法:
用字符分割字符串: 字符串.split();
用字符串分割字符串: Regex.Split();
两者适用情况不同,详细解释如下:
1.1单个字符 作为分割,使用.split('')
string str = "select * From table";
string[] sArray = str.Split('e');
foreach(string s in sArray)
{
Console.Write(s + "<br>");
};
结果: "s" "l" "ct * From tabl"
1.2 多个字符 作为分割 ,使用.split(new char[]{''})
string str = "select * From table";
string[] sArray = str.Split(new char[2]{'e','r'});
foreach(string s in sArray)
{
Console.Write(s + "<br>");
};
结果:"s" "l" "ct * F" "om tabl"
注:string[] arr = str.Split("o"); 这是一个具有语法错误的语句,不应是字符串,应该为string[] arr = str.Split('o');
2 字符串 作为分割
string str = "and b.testid = 3";
string[] sArray = Regex.Split(str, @"and");
foreach(string s in sArray)
{
Console.Write(s + "<br>");
};
结果: " b.testid = 3"
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(',')文章来源:https://www.toymoban.com/news/detail-410051.html
*6个重载函数详解内容来自于网页:https://www.cnblogs.com/fire-dragon/p/5909666.html文章来源地址https://www.toymoban.com/news/detail-410051.html
到了这里,关于C# 将字符串进行分割的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!