How to Compare two Qstrings?

92,499

Solution 1

It worked after Rebuilding the Project , I think this is the problem with QT CREATOR

Solution 2

You can use :

int x = QString::compare(str1, str2, Qt::CaseInsensitive);  // if strings are equal x should return 0

Solution 3

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

The == operator is overloaded for QStrings, as documented here.

I don't know why your code is not working. Recheck other parts of your code.

Share:
92,499
krohit
Author by

krohit

Software working &amp; lifecycle enthusiast .

Updated on August 19, 2021

Comments

  • krohit
    krohit almost 3 years

    I have to compare two Qstrings in qt,

    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.