|
从QList <QString>继承而来,它提供快速索引为基础的接入以及快速插入和清除 其成员函数用于操作这个字符串列表如 append(),insert(), replace(), removeAll(), removeAt(), removeFirst(), removeLast(), and removeOne()等 1. 增加字符串
可以通过append(),或使用<<,如
QStringList fonts; fonts << “Arial” << “Helvetica” << “Times” << “Courier”; // fonts:[ “Arial” ,”Helvetica”, “Times” , “Courier”]
2. 合并字符串使用join( )
QString str = fonts.join(“,”); // str == “Arial,Helvetica,Times,Courier”
3. 拆分字符串
QString str = ” Arial,Helvetica, ,Times,Courier “; QStringList list1 = str.split(“,”); // list1: [ ” Arial “, ” Helvetica “,” “, ” Times “, ” Courier ” ]
QStringList list2 = str.split(“,”, QString::SkipEmptyParts); // list2: [ ” Arial “, ” Helvetica “, ” Times “, ” Courier ” ] 也就是说如果有QString::SkipEmptyParts,空项不会出现在结果。默认情况下,空项被保留
4. 索引
IndexOf()函数返回给定字符串的第一个出现的索引。 而lastIndexOf()函数,返回字符串的最后一次出现的索引。
5. 替换replaceInStrings()
QStringList files; files << “$QTDIR/src/moc/moc.y” << “$QTDIR/src/moc/moc.l” << “$QTDIR/include/qconfig.h”; files.replaceInStrings(“$QTDIR”, “/usr/lib/qt”); // files: [ “/usr/lib/qt/src/moc/moc.y”, …]
6. 过滤filter()
可以让你提取一个新的列表只包含这些字符串包含一个特定的字符串(或匹配特定正则表达式): QStringList list; list << “Bill Murray” << “John Doe” << “Bill Clinton”; QStringList result; result = list.filter(“Bill”); // result: [“Bill Murray”, “Bill Clinton”]
7.遍历
QList<QString>::Iterator it = user.begin(),itend = user.end(); int i = 0; for (;it != itend; it++,i++){ if (*it == pFindLine->text()){//找到,高亮显示 QModelIndex index = model->index(i); m_pTabList->setCurrentIndex(index); break; } }
|
|
|
|
QT4中QStringList的迭代
jackyhwei 发布于 2011-06-14 11:41 点击:2143次 |
来自:dl-lzx.blog.163.com | QT4中QStringList的迭代遍历
#include <QStringList> #include <QDebug> #include <cassert> /* Some simple examples using QStringList, split and join */ int main() { QString winter = “December, January, February”; QString spring = “March, April, May”; QString summer = “June, July, August”; QString fall = “September, October, November”; QStringList list; // QStringList重载了许多函数和操作符 list << winter; /* append operator 1 */ list += spring; /* append operator 2 */ list.append(summer); /* append member function */ list << fall; qDebug() << “The Spring months are: ” << list[1] ; QString allmonths = list.join(“, “); //将“,”加到QStringList /* from list to string – join with a “, ” delimiter */ qDebug() << allmonths; QStringList list2 = allmonths.split(“, “); //按照“,”将QStringList分割成QString /* split is the opposite of join. Each month will have its own element. */ assert(list2.size() == 12); /* assertions abort the program 此时list2有12个元素 if the condition is not satisfied. */ //下面是各种迭代风格 foreach (QString str, list) { /* Qt 4 foreach loop – similar to Perl/Python and Java 1.5 style for loops. */ qDebug() << QString(” [%1] “).arg(str); } for (QStringList::iterator it = list.begin(); it != list.end(); ++it) { /* C++ STL-style iteration */ QString current = *it; /* pointer-style dereference */ qDebug() << “[[” << current << “]]”; } QListIterator<QString> itr (list2); /* Java 1.2 style Iterator */ while (itr.hasNext()) { /* Java Iterators point inbetween elements. */ QString current = itr.next(); qDebug() << “{” << current << “}”; } return 0; }
补充:QString test_list;
test_list<<“abc”<<“def”; //test_list为{“abc”,”def”}
test_list[1]=”bcd”;//test_list为{“abc”,”bcd”}
(dl-lzx) |
QStringList可以使用QList的所有方法,如:append(), prepend(), insert(), replace(), removeAll(), removeAt(), removeFirst(), removeLast(), and removeOne()。
示例:
- #include <QStringList>
- #include <QTextStream>
-
-
- static QTextStream cout(stdout, QIODevice::WriteOnly);
- int main(int argc, char *argv[])
- {
- QString str = “1,2,3,4,5,6,7,8,9”;
- QStringList strList;
-
- strList = str.split(“,”);
-
- cout << “String list item count: “ << strList.size() << endl;
-
- for(int i = 0; i < strList.size(); i++)
- cout << i + 1 << “:” << strList[i] << endl;
-
- QString str2;
- str2 = strList.join(“,”);
-
- cout << str2 << endl;
-
- return 0;
- }
QStringList fonts; fonts << “Arial” << “Helvetica” << “Times” << “Courier”; QString str = fonts.join(“,”);
QString str = “name1,path1;name2,path2;name3,path3”; QStringList list1 = str.split(“;”); // 注意,如果str是空字符串,list1会增加一个空字符串到列表里,其size=1,我为此吃过苦头~ for(int i=0;i<list1.size();i++) { QStringList list2 = list1[i].split(tr(“,”)); }
|
|