QStringList Class Reference

[QtCore module]

QStringList中的类提供的字符串列表。More…

Methods

  • __init__ (self)
  • __init__ (self, QString i)
  • __init__ (self, QStringList l)
  • append (self, QString str)
  • clear (self)
  • bool contains (self, QString str, Qt.CaseSensitivity cs = Qt.CaseSensitive)
  • int count (self, QString str)
  • int count (self)
  • QStringList filter (self, QString str, Qt.CaseSensitivity cs = Qt.CaseSensitive)
  • QStringList filter (self, QRegExp rx)
  • QString first (self)
  • int indexOf (self, QString value, int from = 0)
  • int indexOf (self, QRegExp rx, int from = 0)
  • insert (self, int i, QString str)
  • bool isEmpty (self)
  • QString join (self, QString sep)
  • QString last (self)
  • int lastIndexOf (self, QString value, int from = -1)
  • int lastIndexOf (self, QRegExp rx, int from = -1)
  • QStringList mid (self, int pos, int length = -1)
  • move (self, int from, int to)
  • prepend (self, QString str)
  • int removeAll (self, QString str)
  • removeAt (self, int i)
  • int removeDuplicates (self)
  • replace (self, int i, QString str)
  • QStringList replaceInStrings (self, QString before, QString after, Qt.CaseSensitivity cs = Qt.CaseSensitive)
  • QStringList replaceInStrings (self, QRegExp rx, QString after)
  • sort (self)
  • swap (self, int i, int j)
  • QString takeAt (self, int i)
  • QString takeFirst (self)
  • QString takeLast (self)

Special Methods

  • QStringList __add__ (self, QStringList other)
  • int __contains__ (self, QString str)
  • __delitem__ (self, int i)
  • __delitem__ (self, slice slice)
  • bool __eq__ (self, QStringList other)
  • QString __getitem__ (self, int i)
  • QStringList __getitem__ (self, slice slice)
  • QStringList __iadd__ (self, QStringList other)
  • QStringList __iadd__ (self, QString value)
  • QStringList __imul__ (self, int by)
  • __len__ (self)
  • QStringList __lshift__ (self, QString str)
  • QStringList __lshift__ (self, QStringList l)
  • QStringList __mul__ (self, int by)
  • bool __ne__ (self, QStringList other)
  • __setitem__ (self, int i, QString str)
  • __setitem__ (self, slice slice, QStringList list)

Detailed Description

Python字符串或Unicode对象的Python列表或QString实例可用于每当一个QStringList预计。

QStringList中的类提供的字符串列表。

QStringList中的继承QList\u003cQString\u003e 。喜欢QList, QStringList中是implicitly shared。它提供了快速基于索引的访问以及快速插入和删除。传递字符串列表作为值参数既快速又安全。

所有QList的功能也适用于QStringList中。例如,您可以使用isEmpty( )来测试列表是否为空,你可以调用函数如append( )prepend( )insert( )replace( )removeAll( )removeAt( )removeFirst( )removeLast()和removeOne( )来修改一个QStringList中。此外, QStringList中提供了一些方便的功能,使操作字符串容易列表:

Adding strings

字符串可以使用被添加到列表中的append()operator+=()和operator<<()函数。例如:

  1. QStringList fonts;
  2. fonts << "Arial" << "Helvetica" << "Times" << "Courier";

Iterating over the strings

遍历一个列表,你可以使用的索引位置,或QList的Java风格和STL风格的迭代器类型:

索引:

  1. for (int i = 0; i < fonts.size(); ++i)
  2. cout << fonts.at(i).toLocal8Bit().constData() << endl;

Java风格的迭代器:

  1. [QStringListIterator]($docs-qstringlist.html#QStringListIterator-typedef) javaStyleIterator(fonts);
  2. while (javaStyleIterator.hasNext())
  3. cout << javaStyleIterator.next().toLocal8Bit().constData() << endl;

STL风格的迭代器:

  1. QStringList.const_iterator constIterator;
  2. for (constIterator = fonts.constBegin(); constIterator != fonts.constEnd();
  3. ++constIterator)
  4. cout << (*constIterator).toLocal8Bit().constData() << endl;

QStringListIterator类只是一个类型定义QListIterator\u003cQString\u003e 。 QStringList中还提供了QMutableStringListIterator类,这是一个类型定义QMutableListIterator\u003cQString\u003e 。

Manipulating the strings

QStringList中提供了几个功能,让你操作一个列表的内容。您可以使用串连在一个字符串列表转换成一个字符串的所有字符串(带有可选的分隔符)的join()函数。例如:

  1. [QString]($docs-qstring.html) str = fonts.join(",");
  2. // str == "Arial,Helvetica,Times,Courier"

打破一个字符串转换成一个字符串列表,使用QString.split( )函数:

  1. QStringList list;
  2. list = str.split(",");
  3. // list: ["Arial", "Helvetica", "Times", "Courier"]

该参数分割可以是单个字符,字符串,或QRegExp

此外,该operator+( )功能可让您连接两个字符串列表为一体。要排序的字符串列表,使用sort()函数。

QString名单中还提供了filter( )函数,它可以让你提取一个新的列表,其中包含只有那些包含特定字符串(或匹配特定的正则表达式)字符串:

  1. QStringList monospacedFonts = fonts.filter([QRegExp]($docs-qregexp.html)("Courier|Fixed"));

contains( )函数告诉你的列表中是否包含给定的字符串,而indexOf( )函数返回给定字符串的第一个匹配项的索引。该lastIndexOf在另一方面( )函数,返回字符串中最后一次出现的索引。

最后,该replaceInStrings( )函数调用QString.replace( )在字符串列表中依次每个字符串。例如:

  1. QStringList files;
  2. files << "$QTDIR/src/moc/moc.y"
  3. << "$QTDIR/src/moc/moc.l"
  4. << "$QTDIR/include/qconfig.h";
  5. files.replaceInStrings("$QTDIR", "/usr/lib/qt");
  6. // files: [ "/usr/lib/qt/src/moc/moc.y", ...]

Method Documentation

  1. QStringList.__init__ (self)

构造一个空字符串列表。

  1. QStringList.__init__ (self, QString i)

构造一个字符串列表,其中包含给定的字符串,str。较长的列表很容易像这样创建:

  1. [QStringList]($docs-qstringlist.html) longerList = ([QStringList]($docs-qstringlist.html)() << str1 << str2 << str3);

See also append( ) 。

  1. QStringList.__init__ (self, QStringList l)

构造的一个副本other字符串列表。

该操作需要constant time因为QStringList is implicitly shared,使得返回一个过程QStringList从非常快的函数。如果共享的实例被改性时,其将被复制(复制写入时) ,而这需要linear time

See also operator=( ) 。

  1. QStringList.append (self, QString str)
  1. QStringList.clear (self)
  1. bool QStringList.contains (self, QString str, Qt.CaseSensitivity cs = Qt.CaseSensitive)

如果列表中包含该字符串,则返回Truestr否则返回False 。搜索不区分大小写,如果cs is Qt.CaseInsensitive;搜索是区分默认敏感。

See also indexOf( )lastIndexOf()和QString.contains( ) 。

  1. int QStringList.count (self, QString str)
  1. int QStringList.count (self)
  1. QStringList QStringList.filter (self, QString str, Qt.CaseSensitivity cs = Qt.CaseSensitive)

返回所有的字符串包含子列表str

If cs is Qt.CaseSensitive(默认值) ,字符串比较是区分大小写的,否则比较不区分大小写。

  1. [QStringList]($docs-qstringlist.html) list;
  2. list << "Bill Murray" << "John Doe" << "Bill Clinton";
  3. [QStringList]($docs-qstringlist.html) result;
  4. result = list.filter("Bill");
  5. // result: ["Bill Murray", "Bill Clinton"]

这等效于

  1. [QStringList]($docs-qstringlist.html) result;
  2. foreach (const [QString]($docs-qstring.html) &str, list) {
  3. if (str.contains("Bill"))
  4. result += str;
  5. }

See also contains( ) 。

  1. QStringList QStringList.filter (self, QRegExp rx)

这是一个重载函数。

返回所有匹配正则表达式的字符串列表rx

  1. QString QStringList.first (self)
  1. int QStringList.indexOf (self, QString value, int from = 0)

返回的第一个精确匹配的索引位置rx在列表中,向前搜索从索引位置from。返回-1,如果没有项目匹配。

默认情况下,这个功能是区分大小写的。

See also lastIndexOf( )contains()和QRegExp.exactMatch( ) 。

  1. int QStringList.indexOf (self, QRegExp rx, int from = 0)

返回第一次出现的索引位置value在列表中,向前搜索从索引位置from。返回-1,如果没有项目匹配。

See also lastIndexOf( )contains()和QList.indexOf( ) 。

  1. QStringList.insert (self, int i, QString str)
  1. bool QStringList.isEmpty (self)
  1. QString QStringList.join (self, QString sep)

加入所有的字符串列表的字符串与由给定的分隔每个元素的单个字符串separator(可以是空字符串) 。

See also QString.split( ) 。

  1. QString QStringList.last (self)
  1. int QStringList.lastIndexOf (self, QString value, int from = -1)

返回的最后一个精确匹配的索引位置rx在列表中,从索引位置向后搜索from。如果from为-1 (默认值) ,该搜索从最后一个项目。返回-1,如果没有项目匹配。

默认情况下,这个功能是区分大小写的。

See also indexOf( )contains()和QRegExp.exactMatch( ) 。

  1. int QStringList.lastIndexOf (self, QRegExp rx, int from = -1)

返回最后一次出现的索引位置value在列表中,从索引位置向后搜索from。如果from为-1 (默认值) ,该搜索从最后一个项目。返回-1,如果没有项目匹配。

默认情况下,这个功能是区分大小写的。

See also indexOf()和QList.lastIndexOf( ) 。

  1. QStringList QStringList.mid (self, int pos, int length = -1)
  1. QStringList.move (self, int from, int to)
  1. QStringList.prepend (self, QString str)
  1. int QStringList.removeAll (self, QString str)
  1. QStringList.removeAt (self, int i)
  1. int QStringList.removeDuplicates (self)

此函数删除重复项列表。条目不必进行排序。他们将保留原来的顺序。

返回删除条目的数量。

此功能被引入Qt的4.5 。

  1. QStringList.replace (self, int i, QString str)
  1. QStringList QStringList.replaceInStrings (self, QString before, QString after, Qt.CaseSensitivity cs = Qt.CaseSensitive)

返回一个字符串列表,其中每个字符串发生了before文本与替换after文本的地方before文字被发现。该before文本匹配大小写敏感或不依赖于cs标志。

例如:

  1. [QStringList]($docs-qstringlist.html) list;
  2. list << "alpha" << "beta" << "gamma" << "epsilon";
  3. list.replaceInStrings("a", "o");
  4. // list == ["olpho", "beto", "gommo", "epsilon"]

See also QString.replace( ) 。

  1. QStringList QStringList.replaceInStrings (self, QRegExp rx, QString after)

这是一个重载函数。

取代了正则表达式的每个实例rx在每个字符串列表的字符串,以after。返回一个引用的字符串列表。

例如:

  1. [QStringList]($docs-qstringlist.html) list;
  2. list << "alpha" << "beta" << "gamma" << "epsilon";
  3. list.replaceInStrings([QRegExp]($docs-qregexp.html)("^a"), "o");
  4. // list == ["olpha", "beta", "gamma", "epsilon"]

对于包含正则表达式capturing parentheses,出现的\1\2,…,在after被替换为rx。帽( 1 ) ,rx。盖(2 ) , …

例如:

  1. [QStringList]($docs-qstringlist.html) list;
  2. list << "Bill Clinton" << "Murray, Bill";
  3. list.replaceInStrings([QRegExp]($docs-qregexp.html)("^(.*), (.*)$"), "\\2 \\1");
  4. // list == ["Bill Clinton", "Bill Murray"]
  1. QStringList.sort (self)

按升序排列(敏感的情况下)的字符串列表。

使用Qt的进行排序qSort( )算法,该算法在运行linear-logarithmic time,即澳(n登录n) 。

如果你想以任意顺序的字符串进行排序,请考虑使用QMap类。例如,您可以使用QMap\u003cQStringQString\u003e创建一个不区分大小写的排序(例如,使用按键作为字符串的小写版本,并作为字符串值) ,或QMap\u003c整数,QString\u003e由一些整数索引的字符串进行排序。

See also qSort( ) 。

  1. QStringList.swap (self, int i, int j)
  1. QString QStringList.takeAt (self, int i)
  1. QString QStringList.takeFirst (self)
  1. QString QStringList.takeLast (self)
  1. QStringList QStringList.__add__ (self, QStringList other)
  1. int QStringList.__contains__ (self, QString str)
  1. QStringList.__delitem__ (self, int i)
  1. QStringList.__delitem__ (self, slice slice)
  1. bool QStringList.__eq__ (self, QStringList other)
  1. QString QStringList.__getitem__ (self, int i)
  1. QStringList QStringList.__getitem__ (self, slice slice)
  1. QStringList QStringList.__iadd__ (self, QStringList other)
  1. QStringList QStringList.__iadd__ (self, QString value)
  1. QStringList QStringList.__imul__ (self, int by)
  1. QStringList.__len__ (self)
  1. QStringList QStringList.__lshift__ (self, QString str)
  1. QStringList QStringList.__lshift__ (self, QStringList l)
  1. QStringList QStringList.__mul__ (self, int by)
  1. bool QStringList.__ne__ (self, QStringList other)
  1. QStringList.__setitem__ (self, int i, QString str)
  1. QStringList.__setitem__ (self, slice slice, QStringList list)