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

posted on 2012-07-21 14:59  YuanSong  阅读(16149)  评论(0编辑  收藏  举报

导航