String 類的Length 屬性能夠獲取字符串的長度。這個長度表示的是數組元素個數,而不是字符串的字節數。同樣索引器表示的也是字符串元素的索引。
1.輸出字符串的字節數
string str="博客園:http://www.cnblogs.com";
Encoding gb=System.Text.Encoding.GetEncoding("gb2312");//轉換編碼
byte[] bytes=gb.GetBytes(str);//獲取字節數組
Console.WriteLine(bytes.Length);
Console.WriteLine(gb.GetByteCount(str));
Console.ReadKey();
//------------------輸出------------------------
30
30
2.索引
string str="博客園:http://www.cnblogs.com";
Console.WriteLine(str.Length);
Console.WriteLine(str[4]);
//-------------輸出-------------------
26
h