Qt 字符串的操作(一)查找 替换 比较

//Qt 控制台字符串的操作(一)//查找//替换//比较//若控制台显示为空白按下回车就会显示
#include <QCoreApplication>
#include<QDebug>
#include<QTextStream>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //基本操作
    QString s1 = "Welcome";
    QString s2;
    s2 = s1 + "to you";
    QString s3 = "Hello ";
    s3 = s3 + "World";
    qDebug() << s2 << endl << s3 << endl;
    QString s4 = "Hello";
    s4.append( " World");
    qDebug() << s4 << endl;
    QString s5;
    s5.sprintf("%s","Welcome to my world");
    qDebug() << s5 << endl;
    QString s6;
    s6 = QString("I'm %1 %2").arg("is").arg("Marco");
    qDebug() << s6 << endl;
    //插入//任意位置
    QString s7("Marco  good");
    s7.insert(6,"is");
    qDebug() << s7 << endl;
    //开头
    QString s8(" is good");
    s8.prepend("Marco");
    qDebug() << s8 << endl;
    //替换
    QString s9("Marco is bad");
    s9.replace("bad","good");
    qDebug() << s9 << endl;
    //移除字符串两端空白
    QString s10("      Marco is good    ");
    s10 = s10.trimmed();
    qDebug() << s10 << endl;
    //移除字符串两端空白并添加一个空白符
    QString s11("      Marco is good    ");
    s11 = s11.simplified();
    qDebug() << s11 << endl;
    //查询字符串内容//查询字符串开头
    QString s12("Welcome to you");
    qDebug() << s12.startsWith("Welcome",Qt::CaseSensitive) << " "
             << s12.startsWith("welcome",Qt::CaseInsensitive) << endl;//true
    //查询字符串结尾
    qDebug() << s12.endsWith("you",Qt::CaseSensitive) << " "
             << s12.endsWith("You",Qt::CaseInsensitive) << endl;//true
    //遍历整个字符串判断内容是否出现过
    qDebug() << s12.contains("to",Qt::CaseSensitive) << " "
             << s12.contains("To",Qt::CaseInsensitive) << endl;
    //< <= >= 等同样适用
    //localeAwareCompare(const QString& ,const QString &)静态成员函数
    //比较两个字符串如果前者小于后者返回负整值,等于返回0,大于返回正整数
    if(QString::localeAwareCompare(s11,s12) < 0)
        qDebug() << "s11 < s12" << endl;
    else if(QString::localeAwareCompare(s11,s12) > 0)
        qDebug() << "s11 > s12" << endl;
    else if(QString::localeAwareCompare(s11,s12) == 0)
        qDebug() << "s11 == s12" << endl;
    //compare()//该函数可指定是否区分大小写比较,其他跟localeAwareCompare()类似
    if(QString::compare(s11,s12,Qt::CaseSensitive) < 0)
        qDebug() << "s11 < s12" << endl;
    else if(QString::compare(s11,s12,Qt::CaseSensitive) > 0)
        qDebug() << "s11 > s12" << endl;
    else if(QString::compare(s11,s12,Qt::CaseSensitive) == 0)
        qDebug() << "s11 == s12" << endl;
    return a.exec();
}
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值