C 中分割字串輸出字元陣列

2021-09-08 20:49:25 字數 3099 閱讀 5965

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() + "

");輸出結果:

aaabbb

ccc2、用多個字元來分隔:

string str="aaajbbbscccjdddseee"; 

string sarray=str.split(new char[2] ); 

foreach(string i in sarray) response.write(i.tostring() + "

"); 

輸出結果:

aaabbb

cccddd

eee3、用單個字元來分隔:

string str="aaajbbbjccc";

string sarray=str.split('j');

foreach(string i in sarray) response.write(i.tostring() + "

");輸出結果:

aaabbb

cccstring arr = str.split("o");

這是乙個具有語法錯誤的語句,split 的 separator 引數應該是 char 或 string,不應是字串。正確的示例:

string str = "technology";

char separator = ;

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 );//返回:

string split = words.split(new char );//返回: 

2. public string split(char separator, int count)

程式**

string split = words.split(new char , 2);//返回:

string split = words.split(new char , 6);//返回: 

3. public string split(char separator, stringsplitoptions options)

程式**

string split = words.split(new char , stringsplitoptions.removeemptyentries);//返回: 不保留空元素

string split = words.split(new char , stringsplitoptions.none);//返回: 保留空元素 

4. public string split(string separator, stringsplitoptions options)

程式**

string split = words.split(new string , stringsplitoptions.removeemptyentries);//返回: 不保留空元素

string split = words.split(new string , stringsplitoptions.none);//返回: 保留空元素 

5. public string split(char separator, int count, stringsplitoptions options)

程式**

string split = words.split(new char , 2, stringsplitoptions.removeemptyentries);//返回: 不保留空元素

string split = words.split(new char , 6, stringsplitoptions.none);//返回: 保留空元素 

6. public string split(string separator, int count, stringsplitoptions options)

程式**

string split = words.split(new string , 2, stringsplitoptions.removeemptyentries);//返回: 不保留空元素

string split = words.split(new string , 6, stringsplitoptions.none);//返回: 保留空元素

需要注意的是沒有過載函式public string split(string separator),所以我們不能像vb.net那樣使用words.split(","),而只能使用words.split(',')

輸出字串

5.連線字串 半形句號 是字串連線符,可以把兩個字串連線成乙個字串。例如7 5 echo str.url 技巧 我們可以使用字串連線符累加字串。例如7 6 第一句我們給 str賦值,str表示字串 php中文社群位址是 第二句表示在 str的值上累加字串 www.phpnet.cn 所以,str最後...

sql中分割字串

set ansi nulls on set quoted identifier on goalter function dbo get strarraystrofindex str varchar 1024 要分割的字串 split varchar 10 分隔符號 index int 取第幾個元素 ...

mapreduce中分割字串

剛學mapreduce沒多久,在預設的輸入分片中,經常要對一行的資料進行劃分,如果資料的格式劃分的清除,在分割這一行資料的時候也就好劃分了,但是有時候,資料的格式並不規整,所以劃分起來就得多寫些 了。例如 204001 01 02 03 07 10 25 07這種資料之間的間距是一樣的都是乙個空格的...