C 字串分割,包括分割多個字元

2021-06-19 04:26:05 字數 1498 閱讀 4649

(1)如果字串格式為

string str="abc#def#hijkl#mn";

string s = str.split(new char );

string s = str.split('#');
eval("") + "#"+eval("")
重點是split中的函式是字元型,不是字串

結果就是

s[0]="abc";

s[1]="def";

s[2]="hijkl";

s[3]="mn";

(2)如果是單純的字串擷取就簡單了,比如:

string str="abcdefg";

string strnew=str.substring(3,3);

結果是strnew="def";

去掉最後乙個字元
s1 = s1.remove(s1.lastindexof(","), 1);
分割多個字元,比如換行, 需要使用正規表示式
命名空間:using system.text.regularexpressions;

string

=regex

.split

(

txt_in

.text

,

"\r\n"

);

如果要分割多種

string s = txt_in.text.split(new char , stringsplitoptions.none);
然後,其實不用stringsplitoptions.none也是可以的即:
string s = txt_in.text.split(new char );

好吧, 其實看msdn的.net庫中的string.split方法是最快的
這個是結合後的方法
string s = txt_in.text.split(new string ,stringsplitoptions.none);

********************===

如果要同時分割 +++,++,+比如a+b++b+++c++e為 abbce
string s = txt_in.text.split(new string ,stringsplitoptions.none);
注意順序, 把數量多的放在前面

C 根據某個字元或者字串分割字串

1,字串通過字元分割比較簡單可以這樣實現 string getary content.split new char 這種分割方式可以是多個字元分割 例如 string getary content.split new char string getary content.split x 單個字元分割...

php分割包含中文的字串

這兩個函式都是獲取子字串,而mb substr 一般在字串中包含中文的情況下使用。其中有個很重要的區別是mb substr 按字來切分字串,不管中英文。例子如下 1 php 2 operation 中文test英文abc 3var dump strlen operation 4 price mb s...

C 字串分割

c 中的字元分割是乙個常見的應用,下面是乙個字串分割的 字串分割 vectorsplit string const string str,const string delimiters else pos delim split str.find delimiters res.push back sp...