QString - 操作字符串

1、 +/+=

void add_to_test()
{
    QString str1 = "hello ";
    QString str2 = "my dotey";
    QString str3 = "hello ";
    QString str4 = "hello ";

    str1 = str1 + "world";
    qDebug() << "str1: " << str1;

    str3 = str3 + str2;
    qDebug() << "str3: " << str3;

    str4 += "baby";
    qDebug() << "str4: " << str4;
}

运行结果:

str1:  "hello world"
str3:  "hello my dotey"
str4:  "hello baby"

2、QString::append()  --- 字符串的末尾追加

void append_test()
{
    QString str1 = "Welcome ";
    QString str2 = "to ";
    str1.append(str2);
    str1.append("you! ");
    qDebug() << "append str1:" << str1;
}

运行结果:

arg_str:  "XiaoZhang was bron in 1999."

QString::arg() -- 字符串组合

类型相对安全,完全支持Unicode,并且允许“%n”参数的顺序

void arg_test()
{
    QString str;
    str = QString("%1 was bron in %2.").arg("XiaoZhang").arg("1999");
    qDebug() << "arg_str: " << str;
}

运行结果:

arg_str:  "XiaoZhang was bron in 1999."

QString::trimmed() --- 移除字符串两端的空白字符

QString::simplified() --- 移除字符串两端的空白字符,使用单个空格字符 “  ”代替字符串中出现的空白。

void trimmed_simplified_test()
{
    QString str = "           hello \t my \n Zhang               ";
    str = str.trimmed();
    qDebug() << "trimmed_str1: " << str;
    QString str2 = "           hello \t my \n Zhang               ";
    str2 = str2.simplified();
    qDebug() << "simplified_str2: " << str2;
}

运行结果

trimmed_str1:  "hello \t my \n Zhang"
simplified_str2:  "hello my Zhang"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值