取得字串中指定位置的字元
string 可以看做是唯讀的char[],
我們可以通過index去訪問字串中的某一個字元
1
2
3
4
string s = "您好! Hello World";
char c = s[0]; //取第一個字元
Console.WriteLine(c); //您
Console.WriteLine(s[4]); //H
如果想要給字串s, 更改其中一個元素
s[0] = 'b';,不能這樣做,
因為是它唯讀的,程式就會報錯,程式會提示告訴你它是唯讀的。
Length 取得字串的字元總數
1
2
string s = "您好! Hello World";
Console.WriteLine($"字串長度為:{s.Length}"); //字串長度為:15
[C# 筆記] string 可以看做是唯讀的char[] by R
[C# 筆記] 特殊字元處理:反斜線/、@符號
[C# 筆記] 字元(Char)常用方法
Book: Visual C# 2005 建構資訊系統實戰經典教本