您的位置:首页 > 编程语言 > C#

C# 中split 的用法

2014-02-28 11:03 323 查看
由于用Java用习惯了,进入C# 还想 inputstring.split("分割符"),哎,头痛啊头痛
C# 中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);
////////////////////////////////////////////////////
String.Split 方法有6个重载函数:
程序代码
1) p lic string[] Split(params char[] separator)
2) p lic string[] Split(char[] separator, int count)
3) p lic string[] Split(char[] separator, StringSplitOptions options)
4) p lic string[] Split(string[] separator, StringSplitOptions options)
5) p lic string[] Split(char[] separator, int count, StringSplitOptions options)
6) p lic string[] Split(string[] separator, int count, StringSplitOptions options)
下边我们通过一些实例来说明下怎么使用(以下string words = "1,2.3,,4";):
1. p lic 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. p lic string[] Split(char[] separator, int count)ITP 个人空间,n:H!C0M/S3U\P
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2);//返回:{"1","2.3,,4"}
string[] split = words.Split(new Char[] { ',', '.' }, 6);//返回:{"1","2","3","","4"}
3. p lic 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. p lic string[] Split(string[] separator, StringSplitOptions options)
程序代码
string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2","3","4"} 不保留空元素
\w1I+Ch%^\}0string[] split = words.Split(new string[] { ",", "." }, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
5. p lic string[] Split(char[] separator, int count, StringSplitOptions options)
程序代码
string[] split = words.Split(new Char[] { ',', '.' }, 2, StringSplitOptions.RemoveEmptyEntries);//返回:{"1","2.3,,4"} 不保留空元素ITP 个人空间1K;e\\? }\? n
string[] split = words.Split(new Char[] { ',', '.' }, 6, StringSplitOptions.None);//返回:{"1","2","3","","4"} 保留空元素
6. p lic 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"} 保留空元素
需要注意的是没有重载函数p lic string[] Split(string[] separator),所以我们不能像VB.NET那样使用words.Split(","),而只能使用words.Split(',')
摘自红色黑客联盟(www.7747.net) 原文:http://www.7747.net/kf/201009/74610.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: