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

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

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

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

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

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

 

相关文章:

  • 2021-04-29
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-25
  • 2021-11-25
猜你喜欢
  • 2021-05-28
  • 2021-09-06
  • 2022-12-23
  • 2021-09-16
  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
相关资源
相似解决方案