比较国际字符串

Comparing international strings

本文关键字:字符串 比较      更新时间:2023-10-16

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

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

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%的正确性,不要忘记规范化你的字符串:

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