QString类学习笔记

QString与int的转换

  • 转数字
    QString str = "999";
    int num = str.toInt(&isOk); //若isOk为true则转换成功
    // num == 999
    
  • 转QString
    QString str_;
    str_ = QString::number(999);
    // str_ == "999"
    

replace() 替换字符

QString &replace(int i, int len, const QString &after); 
//参数:位置索引为i、将替换字符个数、将替换字符串引用,返回类型为字符串引用

QString str("hello world!");
str.replace(6, 5, "wcx"); //从索引6开始将5个字符替换为"wcx"
// str == "hello wcx!"

remove() 移除字符

QString &remove(int i, int len);
//参数:位置索引、移除长度,返回类型为字符串引用
//函数从字符串的i处开始移除len个字符并返回此字符串的引用(若i超出字符串长度,则无意义)

QString str("hello world");
str.remove(5,6);
//str == "hello";

QString str("h e l l o _world"); 
str.remove(QRegExp("\\s"); //使用正则除去空格
//str == "hello_world";

leftJustified() 左对齐填充字符

QString leftJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const; 
//参数:width:填充后字符总个数、 填补的字符、 trunc默认为假,若trunc为假且字符串长度超过width,返回的字符串是这个字符串的复制,若为真且字符串长度超过width,那么这个字符串的复制中超过width长度的任何字符都会被移除,然后返回字符串的复制
//函数将返回一个长度为width(包含填补的字符)的字符串。

QString str("hello world");
QString str_ = str.leftJusttified(16, '!');
// str_ == "hello world!!!!!"
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值