关于string的length

在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.

时间: 2024-11-05 20:40:44

关于string的length的相关文章

WCF常见异常-The maximum string content length quota (8192) has been exceeded while reading XML data

异常信息:The maximum string content length quota (8192) has been exceeded while reading XML data 问题:调用第三方的WCF服务,产生上述异常信息 分析: 在公布WCF host端时,要确保host端以及客户端的设置允许一定大小的数据传输. 如果未设置传输大小,maxStringContentLength默认大小为8192. 1)如果第三方服务未设置maxStringContentLength或者设置的maxS

Performance - Method passes constant String of length 1 to character overridden method

This method passes a constant literal String of length 1 as a parameter to a method, that exposes a similar method that takes a char. It is simpler and more expedient to handle one character, rather than a String. Instead of making calls like: String

The maximum string content length quota (8192) has been exceeded while reading XML data

原文:The maximum string content length quota (8192) has been exceeded while reading XML data 问题场景:在我们WCF服务发布后,我们要确保服务端以及客户端的配置文件允许合适大小的传输设置.笔者在发布WCF服务时,服务端的绑定未做传输大小的设置(采用了默认,maxStringContentLength默认大小为8192),而我们在传输序列化的数据时,大小超过了这个限制. 读取 XML 数据时,超出最大字符串内容

ord() expected string of length 1, but int found

源代码是这样: s=b'^SdVkT#S ]`Y\\!^)\x8f\x80ism' key='' for i in s:     i=ord(i)-16     key+=chr(i^32) print (key) 运行后出现了问题:ord() expected string of length 1, but int found 之所以出现这个问题,是在字符串转换过程中出现了一个小错误,在一系列百度和谷歌后,发现概念还是很迷糊,但是在曙光大佬的解答后,明白了怎么处理,就是在s后加"",

tcl之string操作-length/index/range/replace

Leetcode: Encode String with Shortest Length &amp;&amp; G面经

Given a non-empty string, encode the string such that its encoded length is the shortest. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note: k will be a positive integ

Bash String Manipulation Examples – Length, Substring, Find and Replace--reference

In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable with its value. This feature of shell is called parameter expansion. But parameter expansion has numerous other forms which allow you to expand a parame

java中数组有没有length()方法?string没有lenght()方法?

java中数组有没有length()方法,求数组的长度可以使用数组的length属性. int[] arr={1,2,3,4,5}; int length=arr.length;//求数组的长度 ---------------------------------------------------------------------------------------- String 有length()方法,用来求字符串的长度 String  str="Hello"; int leng

[LeetCode] Encode String with Shortest Length 最短长度编码字符串

Given a non-empty string, encode the string such that its encoded length is the shortest. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note: k will be a positive integ