QString的简单测试程序:
#include <QCoreApplication>
#include <QDebug>
#include <QStringList>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("Utf-8"));
qDebug()<<QObject::tr("以下是编辑字符串操作:")<<endl;
QString str = "hello!";
qDebug()<<QObject::tr("字符串大小:")<<str.size();
str[0] = QChar('H');
qDebug()<<QObject::tr("第一个字符:")<<str[0];
str.append(" Qt");
str.replace(1,4,'i');
str.insert(2,"my");
qDebug()<<QObject::tr("str为: ")<<str;
str = str +"!!!";
qDebug()<<QObject::tr("str为: ")<<str;
str = "hi\r\n Qt!\n";
qDebug()<<QObject::tr("str为: ")<<str;
QString str1 = str.trimmed();
qDebug()<<QObject::tr("str1为: ")<<str1;
QString str2 = str.simplified();
qDebug()<<QObject::tr("str2为: ")<<str2;
str = "Hi,my,,Qt!";
QStringList list = str.split(",",QString::SkipEmptyParts);
qDebug()<<QObject::tr("str拆分后为: ")<<list;
str = list.join(" ");
qDebug()<<QObject::tr("list组合后为: ")<<str;
qDebug()<<QString().isNull(); //返回值为:true
qDebug()<<QString().isEmpty(); //返回值为:true
qDebug()<<QString("").isNull(); //返回值为:false
qDebug()<<QString("").isEmpty(); //返回值为:true
return a.exec();
}
输出为: