QString和德国变形金刚

pun*_*uck 4 c++ qstring qt

我正在使用C++和QT,并且德语变音符号有问题.我有一个像"wirsindmüde"的QString,并希望将其更改为"wir sind mü de",以便在QTextBrowser中正确显示它.

我试着这样做:

s = s.replace( QChar('ü'), QString("ü"));
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

 s = s.replace( QChar('\u00fc'), QString("ü"))
Run Code Online (Sandbox Code Playgroud)

不起作用.

当我循环遍历字符串中的所有字符时,'ü'是两个字符.

有谁能够帮我?

Red*_*edX 7

QStrings是UTF-16.

QString存储一串16位QChars,其中每个QChar对应一个Unicode 4.0字符.(代码值高于65535的Unicode字符使用代理项对存储,即两个连续的QChars.)

所以试试吧

//if ü is utf-16, see your fileencoding to know this
s.replace("ü", "ü")

//if ü if you are inputting it from an editor in latin1 mode
s.replace(QString::fromLatin1("ü"), "ü");
s.replace(QString::fromUtf8("ü"), "ü"); //there are a bunch of others, just make sure to select the correct one
Run Code Online (Sandbox Code Playgroud)