C#中Split用法 转
生活随笔
收集整理的這篇文章主要介紹了
C#中Split用法 转
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉自http://www.cnblogs.com/yugen/
"
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) 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.cnblogs.com/ndxsdhy/archive/2011/07/19/2110662.html
總結
以上是生活随笔為你收集整理的C#中Split用法 转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CAS SSO实践中,目前解决的问题和有
- 下一篇: 把SQL Server 错误日志导出为E