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.inrt(6,"is");
qDebug()<<s7<<endl;koron
//开头
QString s8("is good");
我很好的英文
s8.prepend("Marco");
qDebug()<<s8<<endl;
//替换
QString s9("Marco is bad");
qDebug()<<s9<<endl;
//移除字符串两端空⽩
QString s10("Marco is good");
immed();七年级上册英语磁带
qDebug()<<s10<<endl;
//移除字符串两端空⽩并添加⼀个空⽩符
QString s11("Marco is good");
s11=s11.simplified();
stealing
qDebug()<<s11<<endl;
//查询字符串内容//查询字符串开头
QString s12("Welcome to you");
qDebug()<<s12.startsWith("Welcome",Qt::CaSensitive)<<""
<<s12.startsWith("welcome",Qt::CaInnsitive)<<endl;//true
//查询字符串结尾
qDebug()<&dsWith("you",Qt::CaSensitive)<<""
<&dsWith("You",Qt::CaInnsitive)<<endl;//true
//遍历整个字符串判断内容是否出现过
qDebug()<&ains("to",Qt::CaSensitive)<<""
<&ains("To",Qt::CaInnsitive)<<endl;
//<<=>=等同样适⽤
//localeAwareCompare(const QString&,const QString&)静态成员函数
/
/⽐较两个字符串如果前者⼩于后者返回负整值,等于返回0,⼤于返回正整数
if(QString::localeAwareCompare(s11,s12)<0)
qDebug()<<"s11<s12"<<endl;
孟晓驷el if(QString::localeAwareCompare(s11,s12)>0)
qDebug()<<"s11>s12"<<endl;
el if(QString::localeAwareCompare(s11,s12)==0)
qDebug()<<"s11==s12"<<endl;
2011天津中考物理//compare()//该函数可指定是否区分⼤⼩写⽐较,其他跟localeAwareCompare()类似if(QString::compare(s11,s12,Qt::CaSensitive)<0)
qDebug()<<"s11<s12"<<endl;
el if(QString::compare(s11,s12,Qt::CaSensitive)>0)
sunlikeqDebug()<<"s11>s12"<<endl;
el if(QString::compare(s11,s12,Qt::CaSensitive)==0)
qDebug()<<"s11==s12"<<endl;
();
}