1. 程式人生 > >軟件版本號比較(字符串比較)

軟件版本號比較(字符串比較)

gif htm compare 軟件 ota string turn pan value

7.版本號比較(字符串比較)

技術分享圖片
int CompareVersion(QString strVer1, QString strVer2)
{
    if ( !strVer1.compare( strVer2 ) )
    {
        return 0;
    }

    QStringList list1 = strVer1.split( "." );
    QStringList list2 = strVer2.split( "." );

    int iTotal1 = list1.count();
    int iTotal2 = list2.count();

    int iTotal=iTotal1>iTotal2?iTotal2:iTotal1;

    int iValue1 = 0, iValue2 = 0;
    bool ibOK1 = false, ibOK2 = false;
    for ( int iNum = 0; iNum < iTotal; ++iNum )
    {
        iValue1 = list1[iNum].toInt( &ibOK1 );
        if (!ibOK1)
        {
            iValue1=0;
        }

        iValue2 = list2[iNum].toInt( &ibOK2 );
        if (!ibOK2)
        {
            iValue2=0;
        }

        if (iValue1==iValue2)
        {
            continue;
        }
        else if ( iValue1 < iValue2 )
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }

    return iTotal1<iTotal2?-1:1;
}
技術分享圖片

測試:

CompareVersion("16.01.19", "16.01.19") //return 0;
CompareVersion("16.01.19", "16.01.18") //return 1;
CompareVersion("16.01.18", "16.01.19") //return -1;

https://www.cnblogs.com/sz-leez/p/6537004.html

軟件版本號比較(字符串比較)