mid()函数
原型
QString QString::mid(int position, int n = -1) const
返回一个从position开始,长度为n的QString 类型的子串。
当position的下标值超出字符串长度时,返回null;当从position开始的子串长度不够n或n为-1(缺省时的默认值也为-1),函数返回从position开始到结尾的子串。
Returns a string that contains n characters of this string, starting
at the specified position index. Returns a null string if the position
index exceeds the length of the string. If there are less than n
characters available in the string starting at the given position, or
if n is -1 (default), the function returns all characters that are
available from the specified position.
例子:
QString x = "Nine pineapples";
QString y = x.mid(5, 4); // y == "pine"
QString z = x.mid(5); // z == "pineapples"
midRef()
若需要QStringRef类型的子串,可以使用函数midRef (),与mid()类似,只是返回类型为ref,原型如下
QStringRef QString::midRef(int position, int n = -1) const
返回一个从position开始,长度为n的类型的子串的引用。
当position的下标值超出字符串长度时,返回null引用;当从position开始的子串长度不够n或n为-1(缺省时的默认值也为-1),函数返回从position开始直到结尾的子串。
Returns a substring reference to n characters of this string, starting
at the specified position. If the position exceeds the length of the
string, a null reference is returned. If there are less than n
characters available in the string, starting at the given position, or
if n is -1 (default), the function returns all characters from the
specified position onwards.
Example:
QString x = "Nine pineapples";
QStringRef y = x.midRef(5, 4); // y == "pine"
QStringRef z = x.midRef(5); // z == "pineapples"
QString 还有left() 和 right().两个函数可以取子串
left() 与 right()
原型
QString QString::left(int n) const
返回最左边n个字符的子串。当n大于等于QString.size()或n小于0的时候,返回整个字符串。
Returns a substring that contains the n leftmost characters of the
string. The entire string is returned if n is greater than or equal to
size(), or less than zero.
例子:
QString x = "Pineapple";
QString y = x.left(4); // y == "Pine"
right()与left()类似,不阐述