C++ 如何比较两个 Qstring?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19264712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Compare two Qstrings?
提问by krohit
I have to compare two Qstrings in qt,
我必须比较 qt 中的两个 Qstring,
say,
说,
Qstring str1="1005",str2="1006";
I have tried using ,
我试过使用,
if(str1==str2){
return true;
}
&
&
if(str1.compare(str2)==0)
{
return true;
}
still both methods goes inside if condition & returns true.
如果条件&返回true,这两种方法仍然会进入。
采纳答案by krohit
It worked after Rebuilding the Project , I think this is the problem with QT CREATOR
它在重建项目后工作,我认为这是 QT CREATOR 的问题
回答by mcelik
You can use :
您可以使用 :
int x = QString::compare(str1, str2, Qt::CaseInsensitive); // if strings are equal x should return 0
回答by Barath Ravikumar
The code below works fine for me.
下面的代码对我来说很好用。
int main(int argv, char **args)
{
QString str1="1005",str2="1006";
if(str1 == str2)
qDebug()<<"This should not print";
qDebug()<<"Everything Ok";
}
Output:
输出:
Everything Ok
I don't know, why your code is not working, when is should have been fine. Recheck other parts of your code.
我不知道,为什么你的代码不起作用,什么时候应该没问题。重新检查代码的其他部分。