QString general method of summary
- 2020-06-19 11:26:20
- OfStack
Introduction to the
The QString string is used by every GUI program, not only for the user interface, but also for the data structure.
The C++ native provides two types of strings: the traditional C style character array ending in '\0' and the std::string class. Unlike these, QString USES the 16-ES14en Unicode value
When using QString, we don't have to worry about such arcane details as allocating enough memory or having data ending in '\0'. In summary, QString can be thought of as an QChar vector. 1 QString can embed the '\0' character. The length() function returns the entire string size, including the embedded '\0'.
Append string
QString provides a 2 yuan + operator to concatenate two strings and a += operator to append a string to a string.
Such as:
QString str = "A" ;
QString str2 = "D";
str = str + "B";
str2 += "E";
The result is that str is "AB" and str2 is "DE".
Combined string
(1) Use the sprintf() function of QString
Such as:
QString str ;
str.sprintf("%s %.1f%%", "Value", 100.0);
As a result, str was assigned the value "Value 100.0%".
(2) Using arg ()
Such as:
QString str;
str = QString("%1,%2").arg("aa").arg("bb");
Results str is "aa,bb"
Numbers are converted to strings
(1) Use the static function QString::number()
Such as:
str = QString::number(38.5);
(2) Use the setNum() function
str.setNum(38.5);
Converts a string to a number
toInt(), toLongLong(), toDouble()... And so on.
Such as:
QString str = "12";
int i = str.toInt();
Extract string
(1) The mid() function returns a string of a given starting position (first argument) and length (second argument).
Such as:
QString str = "white man";
QString str2 = str.mid(6, 2);
Results str2 is "ma"
(2) mid() returns a substring from the specified starting position to the end of the string if the second argument is omitted.
QString str = "white man";
QString str2 = str.mid(6);
Results str2 was "man"
(3) The left() function returns the first n characters
QString str = "white man";
QString str2 = str.left(5);
Result str2 is "white"
(4) The right() function returns the last n characters
QString str = "white man";
QString str2 = str.right(3);
Results str2 is "man"
Determines whether the string contains
If we want to find out if a string contains a character, string, or regular expression, we can use the indexOf() function of QString.
QString str ;
str.sprintf("%s %.1f%%", "Value", 100.0);
0
As a result, i is 6 and -1 if it does not exist.
Determines whether a string begins or ends with something
(1) Start with something, using the startsWith() function
QString str ;
str.sprintf("%s %.1f%%", "Value", 100.0);
1
The result is i = 1;
(2) End with something, using the endsWith() function
QString str ;
str.sprintf("%s %.1f%%", "Value", 100.0);
2
The result is i = 1;
Case switching
(1) Switch to uppercase, using the toUpper() function
QString str = "FileName";
QString str2 = str.toUpper();
Results str2 is "FILENAME"
(2) Switch to lowercase and use the toLower() function
QString str ;
str.sprintf("%s %.1f%%", "Value", 100.0);
4
Results str2 is "filename"
String substitution
(1) Replace one part of another string with one, using the replace() function
QString str ;
str.sprintf("%s %.1f%%", "Value", 100.0);
5
Results str was "Black man".
(2) Delete m characters starting from position n and use the remove(n,m) function
QString str ;
str.sprintf("%s %.1f%%", "Value", 100.0);
6
Results str was "white"
(3) Insert a string at position n, using the insert(int, QString) function
QString str ;
str.sprintf("%s %.1f%%", "Value", 100.0);
7
Results str was "white strong man"
Filter white space character
Remove whitespace characters from both ends of the string, using trimmed();
QString str = " white man ";
QString str2 = str.trimmed();
Results str2 is "white man"
Split string
1 string can be divided into 1 QStringList with substring by using QString::split()
QString str ;
str.sprintf("%s %.1f%%", "Value", 100.0);
9
Results color. at(1) was "white", ES240en. at(2) was "black", color. at(3) was "yellow".
Determines whether the string is empty
Calling isEmpty() or checking that length() is 0 will do the trick.
if(str.isEmpty()){}
if(str.length() == 0){}
const char * convert to QString
(1) Implicit conversion (automatic)
Such as:
str = "( 121 )";
(2) Explicit conversion
Simply use 1 QString cast, or call the fromAscii() or fromLatin1() function
QString to const char *
Use toAscii() or toLatin1(). These functions return 1 QByteArray, which can be converted to 1 const char * by using either QByteArray::data() or QByteArray::constData().
Such as:
QString = "(1178)"
str.toAscii().data();