C 字串操作

2022-02-25 02:46:59 字數 3686 閱讀 9387

1.字串訪問

string a="

abcd";

console.writeline(a[

0]); //

輸出 a

console.writeline(a.length); //

輸出 4(int型)

2.將字串打散為字元陣列( tochararray() )

string a="

abcd";

char arr=a.tochararray(); //

將a字串打散為 並賦值給字元陣列 arr

console.writeline(arr.length); //

輸出 4

3.字串擷取 ( substring() )

string a = "

abcdefg

"console.writeline(a.substring(

1)); //

從第二位開始擷取 輸出 "bcdefg"

console.writeline(a.substring(2,3)); //

從第三位開始擷取,擷取3個字元 輸出 "cde"

4.匹配索引 ( indexof()、lastindexof()   )

string str = "

abcabcdefg";

//從字串頭部開始搜尋,第乙個匹配字元"a"在字串 str 中的位置索引。 輸出0

console.writeline(str.indexof("a"

));

//從字串頭部開始搜素,第乙個匹配字串"abcd"在字串str中的位置索引。 輸出3

console.writeline(str.indexof("

abcd

"));

//從字串尾部開始搜尋,第乙個匹配字串"a"在字串str中的位置索引。 輸出3

console.writeline(str.lastindexof("a"

));

//從字串尾部開始搜尋,第乙個匹配字串"ab"在字串str中的位置索引。 輸出3

console.writeline(str.lastindexof("ab"

));

//如果字串不存在,返回-1

console.writeline(str.indexof("m"

));

//判斷是否存在字串"acbc" 存在輸出true

console.writeline(str.contains("

abcd

"));

5.大小寫轉換( toupper() 和 tolower() ) 

string a = "

abcd";

console.writeline(a.tolower());

//轉換為小寫 輸出"abcd"

console.writeline(a.toupper()); //

轉換為大寫 輸出"abcd"

6.插入和刪除(insert() 和 remove())

string s = "

abcd";

console.writeline(s.insert(

1,"bc

"));//

從字串的第二位處插入字串"bc",輸出"abbccd"

console.writeline(s); //

輸出"abcd"

//所以 insert() 函式不會改變操作字串的值,如果需要改變,因該進行賦值操作。

console.writeline(s.remove(1)); //

從第2位一直刪除到字串末尾 輸出"a"

console.writeline(s.remove(1,2)); //

從第二位開始刪除,刪除兩個字元 。 輸出"ad"

7.字串替換函式( replace() )

string str = "

a,b,c,d,e";

//將 str字串中 所以"," 替換成 "-"

console.writeline(str.replace("

,","

-"));//

輸出 "a-b-c-d-e"

8.字串分割函式( split() )

s ="

aa,bb,cc,dd";

string arr1 = s.split('

,'); //

以','字元對字串進行分割,返回字串陣列

console.writeline(arr1[0]); //

輸出"aa"

console.writeline(arr1[1]); //

輸出"bb"

console.writeline(arr1[2]); //

輸出"cc"

console.writeline(arr1[3]); //

輸出"dd"

//以字串進行分割的技巧:先把字串"--"替換為單個字元"-",然後以'-'字元對字串進行分割,返回字串陣列

s ="

aa--bb--cc--dd";

string arr2 = s.replace("

--", "

-").split('-'

);console.writeline(arr2[

0]); //

輸出"aa"

console.writeline(arr2[1]); //

輸出"bb"

console.writeline(arr2[2]); //

輸出"cc"

console.writeline(arr2[3]); //

輸出"dd"

9.格式化靜態方法

console.writeline(string.format("

+ =

", 1, 2, 1+2)); //

輸出"1+2=3"

console.writeline(string.format("

/ =

", 1, 3, 1.00/3.00

));console.writeline(

string.format("

", datetime.now));

10,鏈結字串

//

s ="

a,b,c,d";

string arr3 = s.split('

,'); //

arr =

console.writeline(string.concat(arr3)); //

將乙個字串陣列連線成乙個字串,輸出"abcd"

console.writeline(string.join("

,", arr3)); //

以","作為分割符號將乙個字串陣列連線成乙個字串,輸出"a,b,c,d"

stringbuilder sb =new stringbuilder(); //

宣告乙個字串構造器例項

a"); //

使用字串構造器連線字串能獲得更高的效能b'

);console.writeline(sb.tostring());

//輸出"ab"

c 字串操作

獲得漢字的區位碼 bytearray newbyte 2 求字串長度 求字串長度 int len string inputstring 檢測含有中文字串的實際長度 str為要檢測的字串 asciiencoding n new asciiencoding byte b n.getbytes str i...

C 字串操作

1.根據單個分隔字元用split擷取 例如複製 如下 string st gt123 1 string sarray st.split 即可得到sarray 0 gt123 sarray 1 1 2.利用多個字元來分隔字串 例如複製 如下 string str gtazb jiangjben 123...

C字串操作

c字串操作 注 文中的幾個大小寫不敏感比較函式,原文用的是stricmp等,後來發現linux的std庫沒有,改為strcasecmp系列。函式名 strcpy 功 能 拷貝乙個字串到另乙個字串 用 法 char strcpy char destin,char source 程式例 i nclude...