在介绍内容之前,先说明一下开发环境,如下图:
Qt版本:Qt5.3.2;
Qt开发工具:Qt Creater 3.2.1;
Qt构建工具:Desktop Qt 5.3 MinGW 32bit;
Qt开发平台:Windows 7 64bit。
今天介绍QString的修改部分的功能,之前的内容可以参考以下链接。
0022:Qt常用类 - QString(01,QString的编码方式和初始化)
0023:Qt常用类 - QString(02,增加)
0024:Qt常用类 - QString(03,删除)
下面是修改部分的示例代码。
/*
* fill(QChar ch, int size = -1)
* replace(int position, int n, const QString &after)
* replace(int position, int n, const QChar *unicode, int size)
* replace(int position, int n, QChar after)
* replace(const QString &before, const QString &after, Qt::CaseSensitivity cs)
* replace(QChar ch, const QString &after, Qt::CaseSensitivity cs)
* replace(QChar before, QChar after, Qt::CaseSensitivity)
* truncate(int position)
* trimmed()
* operator+=(const QString &other)
* operator+=(QChar ch)
*/
//fill(QChar ch, int size = -1)
QString str1 = "12345一二三四五";
str1.fill(QChar('8'));
qDebug("fill all, str1 = %s, length = %d", qPrintableU8(str1), str1.length());
str1.fill(QChar('7'), 5);
qDebug("fill resize, str1 = %s, length = %d", qPrintableU8(str1), str1.length());
//replace(int position, int n, const QString &after)
QString str2 = "12345一二三四五";
str2.replace(5, 10, "67890六七八九零");
qDebug("str2 = %s, length = %d", qPrintableU8(str2), str2.length());
//replace(int position, int n, const QChar *unicode, int size)
QString str3 = "12345一二三四五";
QChar pCharNormal[10] = { '1', '2', '3', '4', '5', NASCII_UC("一"), NASCII_UC("二"), NASCII_UC("三"), NASCII_UC("四"), NASCII_UC("五") };
str3.replace(5, 10, pCharNormal, 5);
qDebug("str3 = %s, length = %d", qPrintableU8(str3), str3.length());
//replace(int position, int n, QChar after)
QString str4 = "12345一二三四五";
str4.replace(5, 10, QChar('6'));
qDebug("str4 = %s, length = %d", qPrintableU8(str4), str4.length());
//replace(const QString &before, const QString &after, Qt::CaseSensitivity cs)
QString str5 = "12345一二三四五";
str5.replace("一二三四五", "67890");
qDebug("str5 = %s, length = %d", qPrintableU8(str5), str5.length());
//replace(QChar ch, const QString &after, Qt::CaseSensitivity cs)
QString str6 = "12345一二三四五";
str6.replace(QChar('5'), "567890");
qDebug("str6 = %s, length = %d", qPrintableU8(str6), str6.length());
//replace(QChar before, QChar after, Qt::CaseSensitivity)
QString str7 = "12345一二三四五";
str7.replace(QChar('5'), QChar('6'));
qDebug("str7 = %s, length = %d", qPrintableU8(str7), str7.length());
//truncate(int position)
QString str8 = "12345一二三四五";
str8.truncate(5);
qDebug("str8 = %s, length = %d", qPrintableU8(str8), str8.length());
//trimmed()
QString str9 = "12345 一二三四五 ";
str9.trimmed();
qDebug("str9 = %s, length = %d", qPrintableU8(str9), str9.length());
//operator+=(const QString &other)
QString str10 = "12345一二三四五";
str10 += "67890六七八九零";
qDebug("str10 = %s, length = %d", qPrintableU8(str10), str10.length());
//operator+=(QChar ch)
QString str11 = "12345一二三四五";
str11 += QChar('6');
qDebug("str11 = %s, length = %d", qPrintableU8(str11), str11.length());
在学习过程中,可以将上述代码放到一个按钮的响应函数中,以调试方式运行,就可以在Qt Creater中的应用程序输出窗口看到输出结果了。