QString如何比较两个汉字的字符串?

taianmonkey 2012-11-04 04:17:03
int n = QString::localeAwareCompare(strName, "欧洲");
一直返回不了0,另外,系统的编码用的是UTF8
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
...全文
2105 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
乔巴好萌 2012-11-04
  • 打赏
  • 举报
回复
int n = QString::localeAwareCompare(strName, QObject::tr("欧洲"));
haltwang 2012-11-04
  • 打赏
  • 举报
回复
还可以这样 你把"欧洲"换成: QString("欧洲") 这样应该就可以了
haltwang 2012-11-04
  • 打赏
  • 举报
回复
那应该是编码问题。 我觉得可能是这样的: "欧洲"是存在代码区的,所以你用setcodeC是没用的。你得在你的编译器里该编辑框的编码格式
taianmonkey 2012-11-04
  • 打赏
  • 举报
回复
strName没有任何问题 我试过了,不行
haltwang 2012-11-04
  • 打赏
  • 举报
回复
建议把strName打印出来看看,是不是strName这里出了问题。 另外在文档里找的: Compares s1 with s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2. If cs is Qt::CaseSensitive, the comparison is case sensitive; otherwise the comparison is case insensitive. Case sensitive comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-visible strings with localeAwareCompare(). int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive); // x == 0 int y = QString::compare("auto", "Car", Qt::CaseSensitive); // y > 0 int z = QString::compare("auto", "Car", Qt::CaseInsensitive); // z < 0

16,815

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧