c 6 字串 插值 C 內插字串的簡單使用

2021-10-16 14:33:22 字數 4278 閱讀 8187

(1) 字串文字以 $ 字元開頭,後接左雙引號字元。 $ 符號和引號字元之間不能有空格。

(2) 內插字串表示式的結果可以是任何資料型別。

(3) 可通過在內插表示式後接冒號(「:」)和格式字串來指定格式字串。

static void main(string args)

var name = "小鬍子";

var age = 26;

var email = "[email protected]";

var salary = 3700.21;

var today = datetime.now;

console.writeline($"我叫, 今年歲,我的郵箱是! 現在時間是");

//左對齊, 右對齊, 貨幣格式, 三元表示式

console.writeline($"姓名 \r\n工資 \r\n類別 ");

console.readkey();

執行結果:

staticvoid main(string args)

string s ="";

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

s ="abcd";

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

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

console.writeline();

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

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 中內插字串的使用( )

內插字串是c 6.0中引入的新的語法,它允許在字串中插入表示式。相比string.format 它不需要格式字串中的序號與params陣列中的位置對應,可讀性高,便於閱讀。內插字串以 開頭,它可以直接在括號中編寫c 表示式。內插字串內可以繼續插入字串。class program name strin...

C 中的字串內插

特殊字元將字串文字標識為內插字串。內插字串是可能包含內插表示式的字串文字。將內插字串解析為結果字串時,帶有內插表示式的項會替換為表示式結果的字串表示形式。此功能在 c 6 及該語言的更高版本中可用。與使用字串復合格式設定功能建立格式化字串相比,字串內插提供的語法更具可讀性,且更加方便。下面的示例使用...

C 內插字串中數字保留指定位數

在內插字串中,有時候需要將數字保留特定位數顯示,比較常用的方法是使用下面 var number 10 console.writeline 這是用來測試的數字 可以將數字保留兩位小數進行顯示,如果不足兩位則補0,如果想在小數點前保留特定位數,可以這樣寫 var number 10 console.wr...