在介绍内容之前,先说明一下开发环境,如下图: 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,删除) 0025:Qt常用类 - QString(04,修改)
下面是查询部分的示例代码。
/* * contains(const QString &str, Qt::CaseSensitivity cs) * contains(QChar ch, Qt::CaseSensitivity cs) * endsWith(const QString &s, Qt::CaseSensitivity cs) * endsWith(QChar c, Qt::CaseSensitivity cs) * indexOf(const QString &str, int from, Qt::CaseSensitivity cs) * indexOf(QChar ch, int from, Qt::CaseSensitivity cs) * lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) * lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) * startsWith(const QString &s, Qt::CaseSensitivity cs) * startsWith(QChar ch, Qt::CaseSensitivity cs) */ //contains(const QString &str, Qt::CaseSensitivity cs) QString strQuery = "abcdeABCDE一二三四五"; qDebug("strQuery.contains(\"abcdE\") = %d.", strQuery.contains("abcdE")); //contains(QChar ch, Qt::CaseSensitivity cs) qDebug("strQuery.contains(QChar('e')) = %d.", strQuery.contains(QChar('e'))); //endsWith(const QString &s, Qt::CaseSensitivity cs) qDebug("strQuery.endsWith(\"一二三四五\") = %d.", strQuery.endsWith("一二三四五")); //endsWith(QChar c, Qt::CaseSensitivity cs) qDebug("strQuery.endsWith(QChar('e')) = %d.", strQuery.endsWith(QChar('e'))); //indexOf(const QString &str, int from, Qt::CaseSensitivity cs) qDebug("strQuery.indexOf(\"一二三四五\") = %d.", strQuery.indexOf("一二三四五")); //indexOf(QChar ch, int from, Qt::CaseSensitivity cs) qDebug("strQuery.indexOf(QChar('e')) = %d.", strQuery.indexOf(QChar('e'))); //lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) qDebug("strQuery.lastIndexOf(\"一二三四五\") = %d.", strQuery.lastIndexOf("一二三四五")); //lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) qDebug("strQuery.lastIndexOf(QChar('e')) = %d.", strQuery.lastIndexOf(QChar('e'))); //startsWith(const QString &s, Qt::CaseSensitivity cs) qDebug("strQuery.startsWith(\"abcde\") = %d.", strQuery.startsWith("abcde")); //startsWith(QChar ch, Qt::CaseSensitivity cs) qDebug("strQuery.startsWith(QChar('e')) = %d.", strQuery.startsWith(QChar('e')));在学习过程中,可以将上述代码放到一个按钮的响应函数中,以调试方式运行,就可以在Qt Creater中的应用程序输出窗口看到输出结果了。
