1 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);
4)用System.Text.RegularExpressions(框架中的正则表达式来根据字符串截取) string str3="helloMrZhanghelloMissLihelloMrZhao"; string[] strName=System.Text.RegularExpressions.Regex.Split(str3,"hello",System.Text.RegularExpressions.RegexOptions.IgnoreCase); foreach(string var in strName) { Response.Write(var+"<br />"); }
//结果 //MrZhang //MissLi //MrZhao
Regex.Split()中的第2个参数可以是正则表达式
////////////////////////////////////////////////////
String.Split 方法有6个重载函数:
程序代码
1) public string[] Split(params char[]separator)
2) public string[] Split(char[] separator, intcount) 3) public string[] Split(char[] separator, StringSplitOptions options) 4) public string[] Split(string[] separator, StringSplitOptions options) 5) public string[] Split(char[] separator, int count, StringSplitOptionsoptions) 6) public string[] Split(string[] separator, int count, StringSplitOptionsoptions)
2 实例解析
下边我们通过一些实例来说明下怎么使用(以下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, intcount)
程序代码
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, intcount, 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, intcount, 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(',')
3 消除文本中字符串中的空格
有时我们会使用string[] lines=System.IO.File.ReadAllLines(@"c:/root.ini", Encoding.Default);来得到文本中的信息,但是由于文本文件可能不规范,所以取到的字符串数组内容有空格,但是有时字符串有比较或者输出的需要,所以这些空格需要处理掉。
1) 字符串数组中有空格项
如会取到 a[0]="aa"; a[1]=" "; a[2]="321";
处理方案:在取字符串数组的时候,使用RemoveEmptyEntries把结果空白字符串处理掉
用法例:string[] Split(char[]separator, StringSplitOptions.RemoveEmptyEntries)
2)取到的字符串两头有空格
如取到a[0]=" aaa "; a[1]=" ad"; a[2]="ddd ";
处理方案:这样我们可以使用Trim方法来解决这个问题,遍历整个数组,或者指定元素使用
用法例:strings2 = a[0].Trim;
更多Split方法参考: MSDN----Split(中英双语) 更多Trim方法参考:
MSDN----Trim(http://msdn.microsoft.com/zh-cn/library/system.string.trim(VS.80).aspx)
参考:http://www.cnblogs.com/liufei88866/archive/2009/07/03/1516450.html
http://blog.csdn.net/gis_yes/article/details/7761632
http://blog.csdn.net/harrywsc/article/details/6521756 --------------------- 作者:曼陀罗彼岸花 来源:CSDN 原文:https://blog.csdn.net/tiandijun/article/details/40401655 版权声明:本文为博主原创文章,转载请附上博文链接!