Answer the question
In order to leave comments, you need to log in
Remove emojii character from string in Qt?
There is an emoji https://emojipedia.org/tooth/ - you need to remove it from the string.
Unicode taken from here: https://www.fileformat.info/info/unicode/char/1f9b...
Options:
C/C++/Java source code "\uD83E\uDDB7"
if (d->at(k).contains(u8"\\uD83E\\uDDB7"))
if (d->at(k).contains(QString::fromUtf8(QByteArray("f09fa6b7"))))
if (d->at(k).contains("0xd83e")) и if (d->at(k).contains("0xdbd7"))
- just the opposite works, butconst_cast<QString*>(&d->at(k))->remove("\0xd83e");
const_cast<QString*>(&d->at(k))->remove("\0xdbd7");
Answer the question
In order to leave comments, you need to log in
It turned out to remove the characters. The bottom line is that for some reason Qt does not accept the character if you try to remove or replace it, referring to how
U0001F9B7, even if the symbol is definitely present there and is output in this format to the console, via
qDebug()
. QString::fromWCharArray()
- where to pass surrogate pairs QString::fromWCharArray(L"\xD83E\xDDB7");
QString tmpStr=QString::fromWCharArray(L"\xD83E\xDDB7");
myStr.remove(tmpStr);
You already know the answer - specify it as a proper UTF-16 string.
Unicode codepoints above U+FFFF are represented in UTF-16 using a surrogate pair, which is two 16bit codeunits acting together to represent the full Unicode codepoint value. For U+1F50E, the surrogate pair is U+D83D U+DD0E.
In Qt, a UTF-16 codeunit is represented as a QChar, so you need two QChar values, eg:
edit.setText(QString::fromWCharArray(L"\xD83D\xDD0E"));
or:
edit.setText(QString::fromStdWString(L"\xD83D\xDD0E"));
Assuming a platform where sizeof(wchar_t) is 2 and not 4.
In your example, you tried using QString::fromUtf8(), but you gave it an invalid UTF-8 string. ForU+1F50E
, it should have looked like this instead:
edit.setText(QString::fromUtf8("\xF0\x9F\x94\x8E"));
You can also useQString::fromUcs4()
instead:
uint cp = 0x1F50E; edit.setText(QString::fromUcs4(&cp, 1));
void translateUnicodeStr(QString& str)
{
static const QRegExp rx("(\\\\u[0-9a-fA-F]{4})");
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1)
str.replace(pos++, 6, QChar(rx.cap(1).right(4).toUShort(nullptr, 16)));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question