【问题标题】:Split string with emoji用表情符号分割字符串
【发布时间】:2014-11-22 22:13:05
【问题描述】:

我需要将QString 拆分为所有字符的数组。它可以包含emoji
QString:???? ???? ???? ???? ???? ????
string.split("")之后:

""
"?"
"?"
" "
"?"
"?"
" "
"?"
"?"
" "
" "
"?"
"?"
" "
"?"
"?"
" "
"?"
"?"
""

我知道,表情符号可以占用 1 个字节以上,但是我该如何拆分我的字符串呢?谢谢。

【问题讨论】:

  • 任何特定的语言?
  • 我不明白你..这是Qt。
  • Qt 是一个库。具有多种语言的绑定。

标签: c++ qt qstring emoji


【解决方案1】:

据我所知,QString 不支持大于 U+FFFF 的 unicode 字符。在此测试脚本中,您可以看到它无法正确计算这些字符的字符串大小:

#include <QDebug>

int main()
{
    QList<QByteArray> list;
    list << QByteArray("a");
    list << QByteArray("ö");
    list << QByteArray("➜");
    list << QByteArray("☀");
    list << QByteArray("⚡");
    list << QByteArray("?");
    list << QByteArray("?");
    list << QByteArray("?");

    foreach (QByteArray binary, list)
    {
        QString str = QString::fromUtf8(binary);
        qDebug() << str;
        qDebug() << "Bytes:" << binary.size();
        qDebug() << "String size:" << str.size();
        {
            QDebug debugLine = (qDebug() << "Unicode code point:");
            for (int i = 0; i < str.size(); ++i)
            {
                debugLine << str[i].unicode();
            }
        }
        qDebug() << "";
    }

    return 0;
}

输出:

"a"
Bytes: 1
String size: 1
Unicode code point: 97

"ö"
Bytes: 2
String size: 1
Unicode code point: 246

"➜"
Bytes: 3
String size: 1
Unicode code point: 10140

"☀"
Bytes: 3
String size: 1
Unicode code point: 9728

"⚡"
Bytes: 3
String size: 1
Unicode code point: 9889

"?"
Bytes: 4
String size: 2
Unicode code point: 55357 56832

"?"
Bytes: 4
String size: 2
Unicode code point: 55357 57030

"?"
Bytes: 4
String size: 2
Unicode code point: 55357 56373

【讨论】:

  • 那么.. 是不是没有办法拆分字符串,把字符放到QList中?
  • 我目前没有看到一个带有 QString 的。但我不能保证我的研究完全涵盖该主题。
猜你喜欢
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多