Analysis and common functions of QString

QString stores a 16-bit QChar string, where each QChar corresponds to a UTF-16 code unit. (Unicode characters with coded values ​​greater than 65535 are stored using surrogate pairs, that is, two consecutive qchars.)
Unicode is an international standard that supports most writing systems in use today. It is a superset of US-ASCII (ANSI X3.4-1986) and Latin-1 (ISO 8859-1),
and all US-ASCII/Latin-1 characters are available at the same code positions.

insert image description here

QString is a commonly used class in QT programming. In addition to being used as digital input and output, QString has many other functions. Familiarity with these common functions will help you flexibly implement string processing functions.
QString uses Unicode to store strings, and each character is a 16-bit QChar instead of 8-bit char, so QString has no problem processing Chinese characters, and one Chinese character counts as one character.

1. append and prepend

append() adds a string after the string, and prepend() adds a string in front of the string, such as:

QString str1="a", str2="b";
str1.append(str2); //ab

2. toUpper() and toLower()
toUpper() converts all letters in the string to uppercase, and toLower() converts all letters to lowercase, such as:

QString str1 = "Hello, World", str2;
str2 = str1.toUpper(); //str2 = "HELLO, WORLD";

3、count()、size()、length()

count(), size(), and length() all return the number of strings. These three functions are the same, but it should be noted that if there are Chinese characters in the string, one Chinese character counts as one character.

QString str1 = "NI 好";
int N = str1.count(); //N=3
int N = str1.size();//N=3
int N=str1.length(); //N=3

4、trimmed()和simplified()

trimmed() removes the spaces at the beginning and end of the string, and simplified() not only removes the spaces at the beginning and the end, but also replaces the consecutive spaces in the middle with a space.

QString str1 = " Are       you ok?  ", str2;
str2 = str1.trimmed(); //str2="Are     you OK?"
str2=str1.simplified();//str2="Are you OK?"

5、indexOf()和lastIndexOf()

The function prototype of indexOf() is:
int indexOf(const QString &str, int form = 0, Qt::CaseSensitivity cs = Qt::caseSensitive) The
function of const is to check the position of the parameter string str in its own string, The parameter from is the position to start searching, and the QtLLCaseSensitivity cs parameter specifies whether to be case-sensitive.
The lastIndexOf() function checks the last occurrence of a character.

QString str1="my name is xiaoming";
int N=str1.indexOf("name"); //N=3

6. isNull() and isEmpty()

Both functions judge whether the string is empty, but there are slight differences. If an empty string, only "\0", isNull() returns false, and isEmpty() returns true; only an unassigned string, isNull returns true.

QString str1,str2="";
bool N = str1.isNull();//N=true 未赋值的字符串变量
bool N= str2.isNull();//N=false 只有"\0"的字符串,也不是NULL
bool N=str1.isEmpty(); //N=true
bool N=str2.isEmpty(); //N=true

QString will automatically add "\0" at the end of the string as long as it is assigned a value. Therefore, if you just want to judge whether the content in the character is empty, you often use isEmpty().

7、contains()

Determine whether a string contains a certain string, and you can specify whether to be case-sensitive.

QString str1 = "test.cpp";
N=str1.contains(".cpp", Qt::CaseInsensitve);//N=true,不区分大小写
N=str1.contains(".CPP", Qt::CaseSensitive);//N=false,区分大小写

8、endsWith()和startsWith()

startsWith() determines whether it starts with a certain string, and endWith() determines whether it ends with a certain character.

QString str1="test.cpp";
N=str1.endsWith("cpp", Qt::CaseInsensitive);//N=true,不区分大小写
N=str1.startsWith("TEST"); //N=true, 缺省不区分大小写

9. left() and right()

left means how many characters are taken from the left side of the string; right means how many characters are taken from the right side of the string. Note that Chinese characters are treated as one character.

QString str2,str1="我的名字是";
str2 = str1.left(1);//str1="我"

10、section()

The function prototype of section() is:
QString section(const QString &sep, int start, int end=-1, SectionFlags flags=SectionDefault) const
Its function is to extract from the string with sep as the separator, from the start end to the end end string.

QString str2,str1="数学,语文";
str2 = str1.section(",", 0,0) ; //str2="数字" 第一段的编号为0
str2= str1.section(",", 0, 1); //str2="数学,语文"

Guess you like

Origin blog.csdn.net/maokexu123/article/details/130555622