【Qt笔记】[帮助文档]——类QString:取子串函数mid()、left()、right() ——QT怎么取字符串子串切片

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()类似,不阐述

  • 7
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值