在C++里面,std::string的length()返回的是字节数,与编码方式有关。

 int main()
{
std::string s = "我是中国人";
std::cout << s.length() << std::endl;
std::cout << strlen(s.c_str()) << std::endl;
}

上面的代码,使用GB2312编码,输出结果是10和10.

而在C#里面,string.Length属性返回的是字符数,与编码方式无关。

         static void Main(string[] args)
{
string s = "我是中国人";
Console.WriteLine(s.Length);
Console.WriteLine(Encoding.UTF8.GetBytes(s).Length);
}

上面代码输出结果是5和15.

05-06 05:40
查看更多