This article is reproduced from: Use of qstring class in QT -- obtain specified character position, intercept substring, etc_ haiross column - CSDN blog_ qstring interception
Functions in QString class.
1, String concatenation function
1. QString also overloads the + and + = operators. These two operators can connect two strings together.
2. QString's append() function provides similar operations, such as:
str = "User: "; str.append(userName); str.append("\n");
2, Gets the value of a position in a string
QString x = "Nine pineapples"; QString y = x.mid(5, 4); // y == "pine" QString z = x.mid(5); // z == "pineapples"
1. The mid() function accepts two parameters, the first is the starting position and the second is the length of the string. If the second parameter is omitted, it is truncated from the beginning to the end. As the example above shows
2. The functions left () and rigt() are similar. They both accept an int type parameter n, which intercepts the string. The difference is that the left () function intercepts n characters from the left, while right() intercepts from the right. The following is an example of left():
QString x = "Pineapple"; QString y = x.left(4); // y == "Pine"
3, Gets the position of the character in the character
indexOf()
//There is a lastIndexOf() function to return the index of the last occurrence of the string? QString x = "sticky question"; QString y = "sti"; x.indexOf(y); // returns 0 x.indexOf(y, 1); // returns 10 x.indexOf(y, 10); // returns 10 x.indexOf(y, 11); // returns -1
4, You can detect whether a string starts or ends with a specific string.
startsWith() endsWith()
if (url.startsWith("http:") && url.endsWith(".png")) { } This code is equivalent to if (url.left(5) == "http:" && url.right(4) == ".png") { }
5, String replacement function
replace()
The trimmed() function removes white space characters on both sides of the string (note that white space characters include spaces, tabs, and line breaks, not just spaces);
The toLower() and toUpper() functions will convert the string to lowercase and uppercase strings;
The remove() and insert() functions provide the ability to delete and insert strings;
The simplified() function can replace all consecutive white space characters in the string with one, and remove the white space characters at both ends, such as“ \ t ” A space "" will be returned.
6, Conversion between C-style string of const char * type and QString character.
Simply put, + = of QString can complete this function:
str += " (1870)";
Here, we convert the string "(1870)" of const char * type to QString type.
If explicit conversion is required, you can use the cast operation of QString or the function fromAscii().
In order to convert QString type to const char * string, two steps are required. One is to use toAscii() to obtain a QByteArray type object,
Then call its data() or constData() function.
For example:
printf("User: %s\n", str.toAscii().data());
For convenience, Qt provides a macro qPrintable(), which is equivalent to toAscii().constData(), for example:
printf("User: %s\n", qPrintable(str));
We call the data() or constData() function above the QByteArray class to obtain a const char * type string inside QByteArray,
Therefore, we don't need to worry about memory leakage. Qt will manage the memory for us. However, it also implies that we should not use this pointer for too long,
Because if QByteArray is delete d, the pointer will become a wild pointer. If the QByteArray object is not placed in a variable,
Then, when the statement ends, the QbyteArray object will be deleted, and the pointer will be deleted.
7, String and other types of conversion functions
toInt() Turn integer
toDouble() To double precision
toLong() Extended integer
These functions accept a bool pointer as a parameter. After the function is completed, it will be set to true or false according to whether the conversion is successful:
bool ok; double d = str.toDouble(&ok); if(ok) { // do something... } else { // do something... }
Integer to string:
1. Use static's function number() to convert a number into a string. For example:
QString str = QString::number(54.3);
2. You can also use the non static function setNum() to achieve the same purpose:
QString str; str.setNum(54.3);
8, QString provides a sprintf() function, which implements the same function as the printf function in C language.
1.
str.sprintf("%s %.1f%%", "perfect competition", 100.0);
This code will output: perfect competition 100.0%
2. Another function for formatting string output arg():
str = QString("%1 %2 (%3s-%4s)").arg("permissive").arg("society").arg(1950).arg(1970); { QString str; str ="%1 %2"; str.arg("%1f","Hello"); // returns "%1f Hello" str.arg("%1f").arg("Hello"); // returns "Hellof %2" }
In this code,% 1,% 2,% 3,% 4 as placeholders will be replaced in turn by the contents of the following arg() function. For example,% 1 will be replaced with permission,
% 2 will be replaced by society,% 3 will be replaced by 1950,% 4 will be replaced by 1970, and finally,
The output of this code is: permission Society (1950s-1970s). Arg() function is type safe compared with sprintf(),
It also accepts a variety of data types as parameters, so it is recommended to use the arg() function instead of the traditional sprintf().
9, Find the length of the string, and the return value is INT type.
length();
10, How to display Chinese characters correctly
If you want QT to obtain the character set according to the environment variable of Locale, you can use the following command:
QString::fromLocal8Bit("Hello, world!");
Use the fromLocal8Bit() function of QString;
QString str; str = str.fromLocal8Bit("Ha ha ha");