gpt4 book ai didi

c++ - 比较国际字符串

转载 作者:行者123 更新时间:2023-11-27 23:59:46 26 4
gpt4 key购买 nike

我想做的是比较具有特殊字符(法语)的 2 个 QString

首先我从服务器收到的 json 数据保存在 txtInfo 中

txtInfo = "Présenter";

当我遇到这样的情况时,它不会工作(它不会设置状态。)

  if (txtInfo == "Présenter"){
m_appState = 8;
m_appStateString = AppStatesArray[m_appState];
}

else {
m_appState = -1;
m_appStateString = "UNKNOWN";
}

我错过了什么?如果我想比较的不是法语而是中文怎么办?

非常感谢

最佳答案

从 Qt 5 开始,QString 的operator== 对与其进行比较的字符数组执行fromUtf8 转换。但是,如果您的源文件 (.cpp) 未使用 utf8,则您需要构建自己的 QString。

取决于源文件的 (.cpp) 编码:

UTF8:

QString compared = QString::fromUtf8("Présenter");
if (txtInfo == QString::fromUtf8("Présenter")){

本地 8 位:

QString compared = QString::fromLocal8Bit("Présenter");
if (txtInfo == QString::fromUtf8("Présenter")){

为了 100% 正确,不要忘记 normalize你的字符串:

txtInfo = txtInfo.normalized(QString::NormalizationForm_D);
QString compared = /* the correct form for you */;
if (txtInfo == compared.normalized(QString::NormalizationForm_D)){

关于c++ - 比较国际字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40038537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com