//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();
}
Qt 字符串的操作(一)查找 替换 比较
最新推荐文章于 2024-08-19 09:52:22 发布