asp.net字串分割函數使用方法分享_實用技巧

來源:互聯網
上載者:User

先來看個簡單的執行個體

但是其數組長度卻是25,而不是3。下面這種方法是先將“[jb51.net]”替換成一個特殊字元,比如$,在根據這個字元執行Split

例如下面根據[jb51.net]分割

複製代碼 代碼如下:

string[] arrstr2 = str.Replace("[jb51.net]", "$").Split('$');

好了看其它方法、最簡單最常用的方法,以一個指定的字元進行的分割 開啟vs.net建立一個控制台項目。然後在Main()方法下輸入下面的程式。

複製代碼 代碼如下:

string s="abcdeabcdeabcde";
string[] sArray=s.Split('c');
foreach(string i in sArray)
Console.WriteLine(i.ToString());

輸出下面的結果:

複製代碼 代碼如下:

ab
deab
deab
de

2、使用多個字元進行分割

複製代碼 代碼如下:

string s="abcdeabcdeabcde"
string[] sArray1=s.Split(new char[3]{'c','d','e'});
foreach(string i in sArray1)
Console.WriteLine(i.ToString());

輸出下面的結果:

複製代碼 代碼如下:

ab
ab
ab

3、使用Regex

添加引用

複製代碼 代碼如下:

using System.Text.RegularExpressions;

string content="agcsmallmacsmallgggsmallytx";
string[]resultString=Regex.Split(content,"small",RegexOptions.IgnoreCase)
foreach(string i in resultString)
Console.WriteLine(i.ToString());

輸出下面的結果:

複製代碼 代碼如下:

agc
mac
ggg
ytx

還有一種比較不常用的方法

複製代碼 代碼如下:

string str = "reterry[jb51.net]是雲棲社區[jb51.net]的站長";
string[] arrstr = str.Split(new char[] { '[', 's', 'o', 's', 'u', 'o', '8', '.', 'c', 'o', 'm', ']' });
for (int i = 0; i < arrstr.Length; i++)
{
Response.Write(arrstr[i]);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.