Common usage of QString (detailed explanation)
The QString class provides a unicode string. A variable type commonly used in the QT software development platform, which provides many convenient application methods. Here are some common uses of QString.
1. Append at the end of the string
//example 1
QString str="hello";
str.append("world");
qDebug() <<str; //str=hello world
//example 2
QString str="hello";
str=str+"world";
qDebug() <<str; //str=hello world
//example 3
QString str="hello";
str+="world";
qDebug() <<str; //str=hello world
2. Take out the character at the specified position in the string
QString str="hello world";
char ch;
ch=str.at(1).unicode();
qDebug() <<ch; //ch=e
3. Delete several characters from the end of the string
QString str="hello world";
str.chop(2);
qDebug() <<str; //str= hello wor
4. Empty the string
//example 1
QString str="hello world";
str.clear();
qDebug() <<str.size(); //str=""
//example 2
QString str="hello world";
str="";
qDebug() <<str.size(); //str=""
5. String comparison
int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive); // x == 0
int y = QString::compare("aUtO", "AuTo", Qt::CaseSensitive); // x > 0
int z = QString::compare("auto", "Car", Qt::CaseSensitive); // y > 0
int k = QString::compare("auto", "Car", Qt::CaseInsensitive); // z < 0
//CaseInsensitive: case insensitive CaseSensitive: case sensitive
6. Whether the string str1 contains the string str2
QString str1 = "Peter Pan";
QString str2 = "peter";
bool rel;
rel=str1.contains(str2, Qt::CaseInsensitive); //CaseInsensitive: case insensitive
qDebug() <<rel; // returns true
7. The string str2 appears several times in the string str1
QString str1 = "HELLO world hello world";
QString str2 = "hello";
int rel;
rel=str1.count(str2, Qt::CaseInsensitive); //Search is not case sensitive
qDebug() <<rel; // rel=2
8. Does the string str1 end with str2
QString str = "Bananas";
str.endsWith("anas"); // returns true
str.endsWith("pple"); // returns false
9. Search for the first occurrence of the string str2 in the string str1
QString str1 = "sticky question"; //contains 2 "sti"
QString str2 = "sti";
// start searching from position 0
str1.indexOf(str2); // returns 0 (first occurrence of position 0))
// start searching from position 1
str1.indexOf(str2, 1); // returns 10 (first occurrence of position 10))
// start searching from position 10
str1.indexOf(str2, 10); // returns 10 (first occurrence of position 10)
//Start searching from position 11
str1.indexOf(str2, 11); // returns -1 (doesn't exist)
10. Insert str2 in the specified position of string str1
QString str1 = "Meal";
QString str2 = "ontr";
str1.insert(1, str2); //Insert from position 1
// str1 == "Montreal"
11. Determine whether the string str is empty or has no characters
// string has no characters
QString().isEmpty(); // returns true
QString("").isEmpty(); // returns true
QString("x").isEmpty(); // returns false
QString("abc").isEmpty(); // returns false
// string is empty
QString().isNull(); // returns true
QString("").isNull(); // returns false
QString("abc").isNull(); // returns false
12. Calculate the length of the string str
QString str = "hello";
int rel=str.length();
qDebug() <<rel; // rel=5
13. Intercept characters from the specified position in the string str1
QString str1 = "Nine pineapples";
//Start intercepting from position 5, intercepting 4 characters
QString str2 = str1.mid(5, 4); // str2 == "pine"
//Start intercepting from position 5, intercepting to the end
QString str3 = str1.mid(5); // str2 == "pineapples"
14. Remove several characters from the specified position in the string str
QString str = "Montreal";
//Start at position 1, remove 4 characters
str.remove(1, 4);// str == "Meal"
15. Replace several characters from the specified position in the str1 string
QString str1 = "Say yes!";
QString str2 = "no";
//Start at position 4, replace 3 characters
str1.replace(4, 3, str2); // str1 == "Say no!"
16. Divide the string with the specified character, and take out a segment after the division
QString str;
QString csv = "forename,middlename,surname,phone";
QString path = "/usr/local/bin/myapp"; // first field is empty
QString::SectionFlag flag = QString::SectionSkipEmpty;
//With "," as the separator, it will return from the second paragraph from left to right
str = csv.section(',', 2, 2); // str == "surname"
qDebug() <<str;
//With "/" as the delimiter, it will return from the 3rd to the 4th paragraph from left to right
str = path.section('/', 3, 4); // str == "bin/myapp"
qDebug() <<str;
//With "/" as the delimiter, the third paragraph from left to right (ignore the preceding empty field)
str = path.section('/', 3, 3, flag); // str == "myapp"
qDebug() <<str;
//The preceding symbol means counting from right to left
str = csv.section(',', -3, -2); // str == "middlename,surname"
str = path.section('/', -1); // str == "myapp"
//In addition to the single character ',' as the separator, you can also use the "**" string as the separator, you can experiment by yourself
17. Number to String
QString str;
//10 represents converting to a string in decimal (allowed range 2~36)
str.setNum(1234,10); // str == "1234"
//'g' stands for precision
str.setNum(12.34,'g',10); // str == "12.34"
18. Format to string
QString str;
int value=1998;
str.sprintf("value=%d",value);
qDebug() <<str; //str="value=1998"
19. Determine whether the string str starts with the string str2
QString str = "Bananas";
str.startsWith("Ban",Qt::CaseSensitive); // returns true
str.startsWith("ban",Qt::CaseInsensitive); // returns true
str.startsWith("Car",Qt::CaseSensitive); // returns false
20. Convert the string str to a value
//example 1 QString >> double
bool d;
d = QString( "1234,56" ).toDouble(&ok); // ok == false
d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
//exapmle 2 QString >> folat
QString str1 = "1234.56";
str1.toFloat(); // returns 1234.56
bool ok;
QString str2 = "R2D2"
;
str2.toFloat(&ok); // returns 0.0, sets ok to false
//exapmle 3 QString >> int
QString str = "FF";
bool ok;
//16 means hex
int hex = str.toInt(&ok, 16); // hex == 255, ok == true
//10 means decimal conversion failed
int dec = str.toInt(&ok, 10); // dec == 0, ok == false
21. Convert all the letters in the string str to lowercase or uppercase
//example 1 uppercase to lowercase
QString str = "The Qt PROJECT";
str = str.toLower(); // str == "the qt project"
//example 2 lowercase to uppercase
QString str = "TeXt";
str = str.toUpper(); // str == "TEXT"
22. Truncate the string str from the specified position
QString str = "Vladivostok";
str.truncate(4); // str == "Vlad"
23. The operation symbols supported by QString are:
For comparison between strings: "!=" "<" "<=" "==" ">="
For passing between strings: "+=" "="
"Smartness lies in diligence, genius lies in accumulation - Hua Luogeng"
———————————————
Copyright statement: This article is an original article by CSDN blogger "Fragile Code", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/weixin_42653531/article/details/97810964