在Qt中,可以使用QString的compare()函数来比较字符串的大小。该函数有多种重载形式:
示例:
QString str1 = "abc";
QString str2 = "def";
int result = str1.compare(str2);
if(result < 0) {
qDebug() << "str1 is smaller than str2";
} else if(result > 0) {
qDebug() << "str1 is greater than str2";
} else {
qDebug() << "str1 is equal to str2";
}
示例:
QString str1 = "abc";
QString str2 = "ABC";
int result = str1.localeAwareCompare(str2);
if(result < 0) {
qDebug() << "str1 is smaller than str2";
} else if(result > 0) {
qDebug() << "str1 is greater than str2";
} else {
qDebug() << "str1 is equal to str2";
}
需要注意的是,这些函数返回的结果是基于Unicode编码的字符顺序比较的。