获取QString长度(以字符为单位,而不是字节) [英] Get QString length (in characters, not bytes)
问题描述
我需要获取实际的字符数(而不是字节数),类似于在V8中获取字符串长度时输出的内容.
I need to get the actual character count (not byte count), similar to what is output when getting string length in V8.
这对于与Twitter结合使用是必需的,无论使用哪种语言,Twitter都会根据字符数进行计数,即使对于UTF-8(也不按字节长度).
This is necessary for use with Twitter, which goes by character count no matter the language used, even with UTF-8 (it does NOT go by byte length).
例如:
在chrome/chromium js控制台中或在nodejs中:
in chrome/chromium js console, or in nodejs:
> "Schöne Grüße".length
< 12
在Qt 4.8.2中,尝试使用 QString someStr =SchöneGrüße";cout<<someStr.length()
将输出15,这不是我的目标.
In Qt 4.8.2, trying QString someStr = "Schöne Grüße"; cout << someStr.length()
will output 15, which is not what I'm aiming for.
推荐答案
I believe you need to construct it using the specific fromUtf8
static method:
QString s = QString::fromUtf8("Schöne Grüße");
这篇关于获取QString长度(以字符为单位,而不是字节)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!