C#String類Length屬性


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM