c++qtqstring

Get QString length (in characters, not bytes)


I need to get the actual character count (not byte count), similar to what is output when getting string length in V8.

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

Ex:

in chrome/chromium js console, or in nodejs:

> "Schöne Grüße".length
< 12

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.


Solution

  • I believe you need to construct it using the specific fromUtf8 static method:

    QString s = QString::fromUtf8("Schöne Grüße");