比较国际字符串

时间:2016-10-14 08:30:45

标签: c++ string qt qstring qt5.7

我要做的是comparin 2 QStrings有特殊字符(法语)

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

 Message msg = onProcess.get(processId);
 sqs.deleteMessage(new DeleteMessageRequest().withQueueUrl(queueUrl).withReceiptHandle(msg.getReceiptHandle()));
 onProcess.remove(processId);

当我有这样的条件时,它不会起作用(它不会设置状态。)

txtInfo = "Présenter";

我缺少什么?如果我想比较法语而不是中文怎么办?

非常感谢

1 个答案:

答案 0 :(得分:3)

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

取决于您的源文件&#c;(.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)){