在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編碼的字符順序比較的。