C 操作字串方法總結《轉》

2022-05-09 05:27:11 字數 3527 閱讀 5690

staticvoid main(string args)

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"

console.writeline();

//(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

console.writeline();

//(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__"

console.writeline();

//(7)截頭去尾(trim)

s ="__ab__cd__";

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

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

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

console.writeline();

//(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"

console.writeline();

//(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"

console.writeline();

//(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 操作字串方法總結《轉》

static void main string args console.writeline arr 0 輸出陣列的第乙個元素,輸出 a console.writeline 3 擷取子串 substring s abcd console.writeline s.substring 1 從第2位開始 ...

C 操作字串方法總結《轉》

static void main string args console.writeline arr 0 輸出陣列的第乙個元素,輸出 a console.writeline 3 擷取子串 substring s abcd console.writeline s.substring 1 從第2位開始 ...

C 操作字串方法總結《轉》

static void main string args console.writeline arr 0 輸出陣列的第乙個元素,輸出 a console.writeline 3 擷取子串 substring s abcd console.writeline s.substring 1 從第2位開始 ...