C 字串操作

2021-10-07 17:41:32 字數 3747 閱讀 6436

c#字串操作

(1)字元訪問(下標訪問s[i])

s ="abcd";

console.writeline(s[0]); // 輸出"a";

console.writeline(s.length); // 輸出4

(2)打散為字元陣列(tochararray)

s ="abcd";

char arr = s.tochararray(); // 把字串打散成字元陣列

console.writeline(arr[0]); // 輸出陣列的第乙個元素,輸出"a"

console.writeline();

(3)擷取子串(substring)

s ="abcd";

console.writeline(s.substring(1)); // 從第2位開始(索引從0開始)

擷取一直到字 符串結束,輸出"bcd"

console.writeline(s.substring(1, 2)); // 從第2位開始擷取2位,輸出"bc"

(4)匹配索引(indexof())

s ="abcabcd";

console.writeline(s.indexof('a')); // 從字串頭部開始搜尋第乙個匹配字元a的位置索引,輸出"0"

console.writeline(s.indexof("bcd")); // 從字串頭部開始搜尋第乙個匹配字串bcd的位置,輸出"4"

console.writeline(s.lastindexof('c')); // 從字串尾部開始搜尋第乙個匹配字元c的位置,輸出"5"

console.writeline(s.lastindexof("ab")); // 從字串尾部開始搜尋第乙個匹配字串bcd的位置,輸出"3"

console.writeline(s.indexof('e')); // 從字串頭部開始搜尋第乙個匹配字串e的位置,沒有匹配輸出"-1";

console.writeline(s.contains("abcd")); // 判斷字串中是否存在另乙個字串"abcd",輸出true

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

s ="abcd";

console.writeline(s.tolower()); // 轉化為小寫,輸出"abcd"

console.writeline(s.toupper()); // 轉化為大寫,輸出"abcd"

console.writeline();

(6)填充對齊(padleft和padright)

s ="abcd";

console.writeline(s.padleft(6, '_')); // 使用'_'填充字串左部,使它擴充到6位總長度,輸出"__abcd"

console.writeline(s.padright(6, '_')); // 使用'_'填充字串右部,使它擴充到6位總長度,輸出"abcd__"

(7)截頭去尾(trim)

s ="__ab__cd__";

console.writeline(s.trim('_')); // 移除字串中頭部和尾部的'_'字元,輸出"ab__cd"

console.writeline(s.trimstart('_')); // 移除字串中頭部的'_'字元,輸出"ab__cd__"

console.writeline(s.trimend('_')); // 移除字串中尾部的'_'字元,輸出"__ab__cd"

(8)插入和刪除(insert和remove)

s ="adef";

console.writeline(s.insert(1, "bc")); // 在字串的第2位處插入字串"bc",輸出"abcdef"

console.writeline(s);

console.writeline(s.remove(1)); // 從字串的第2位開始到最後的字元都刪除,輸出"a"

console.writeline(s);

console.writeline(s.remove(0, 2)); // 從字串的第1位開始刪除2個字元,輸出"ef"

(9)替換字元(串)(replace)

s ="a_b_c_d";

console.writeline(s.replace('_', '-')); // 把字串中的'_'字元替換為'-',輸出"a-b-c-d"

console.writeline(s.replace("_", "")); // 把字串中的"_"替換為空字串,輸出"a b c d"

console.writeline();

(10)分割為字串陣列(split)——互逆操作:聯合乙個字串靜態方法join(seperator,arr)

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"

console.writeline();

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"

(11)格式化(靜態方法format)

console.writeline(string.format(" +  = ", 1, 2, 1+2));

console.writeline(string.format(" / = ", 1, 3, 1.00/3.00));

console.writeline(string.format("", datetime.now));

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(); // 宣告乙個字串構造器例項

console.writeline(sb.tostring());// 輸出"ab"

console.readkey();

}

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...