C 操作String方法

2021-10-06 18:43:56 字數 3739 閱讀 8901

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));

//(12)連線成乙個字串(靜態方法concat、靜態方法join和例項化

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();

}

String 操作方法

對字串進行操作 stringbuffer sb new stringbuffer 你好 2 sb.reverse 反轉字串,有索引反轉方法 3 sb.delete 刪除索引位置中的字串 4 sb.insert 新增索引位置中的字串 5 sb.replace 替換索引位置的字串 string的方法 c...

C 仿string實現的String方法

c 仿string實現的string方法 string.h include using namespace std class string 拷貝構造 string const string string string 過載等號運算子,接收的是c語言字串 string operator const ...

C 中String 操作 一)

相對於c語言中麻煩的c字串操作,c 中提供了string類。本文將繼續繼承前文的風格,以 為驅動,初步簡介c string類最簡單的部分。string的初始化 include include using namespace std int main 作為一種複雜而龐大的程式語言c 中為很多特定情況下...