QStringList Class
QStringList 类提供了一个字符串列表。更多内容...
| 头文件: | #include <QStringList> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
- 所有成员的列表,包括继承的成员
- QStringList 属于“隐式共享类”和“字符串数据类”范畴。
注意:该类中的所有函数均为可重入函数。
公共函数
| QStringList(const QList<QString> &other) | |
| QStringList(const QString &str) | |
| QStringList(QList<QString> &&other) | |
| bool | contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| bool | contains(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| bool | contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
(since 6.9) QStringList | filter(const QLatin1StringMatcher &matcher) const |
| QStringList | filter(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| QStringList | filter(const QRegularExpression &re) const |
(since 6.7) QStringList | filter(const QStringMatcher &matcher) const |
(since 6.7) QStringList | filter(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| QStringList | filter(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| qsizetype | indexOf(QLatin1StringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| qsizetype | indexOf(QStringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| qsizetype | indexOf(const QString &str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| qsizetype | indexOf(const QRegularExpression &re, qsizetype from = 0) const |
| QString | join(const QString &separator) const |
| QString | join(QChar separator) const |
| QString | join(QLatin1StringView separator) const |
| QString | join(QStringView separator) const |
| qsizetype | lastIndexOf(QLatin1StringView str, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| qsizetype | lastIndexOf(QStringView str, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| qsizetype | lastIndexOf(const QString &str, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| qsizetype | lastIndexOf(const QRegularExpression &re, qsizetype from = -1) const |
| qsizetype | removeDuplicates() |
| QStringList & | replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| QStringList & | replaceInStrings(const QRegularExpression &re, const QString &after) |
| QStringList & | replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| QStringList & | replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| QStringList & | replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| void | sort(Qt::CaseSensitivity cs = Qt::CaseSensitive) |
| QStringList | operator+(const QStringList &other) const |
| QStringList & | operator<<(const QString &str) |
| QStringList & | operator<<(const QList<QString> &other) |
| QStringList & | operator<<(const QStringList &other) |
| QStringList & | operator=(const QList<QString> &other) |
| QStringList & | operator=(QList<QString> &&other) |
相关的非成员
详细说明
QStringList 是一个QList ,并添加了一些用于处理字符串列表的便捷函数。与QList 一样,QStringList 是隐式共享的。它提供了基于索引的高速访问,以及快速的插入和删除操作。将字符串列表作为值参数传递既快速又安全。
QList 的所有功能同样适用于 QStringList。例如,您可以使用isEmpty() 来检测列表是否为空,并可调用append()、prepend()、insert()、replace()、removeAll()、removeAt()、removeFirst()、removeLast() 以及removeOne() 等函数来修改 QStringList。
初始化
默认构造函数会创建一个空列表。您可以使用初始化列表构造函数来创建包含元素的列表:
QStringList fonts = { "Arial", "Helvetica", "Times" };添加字符串
可以使用insert()、append()、operator+=() 和operator<<() 函数将字符串添加到列表中。
operator<<() 可用于便捷地向列表中添加多个元素:
fonts << "Courier" << "Verdana";遍历字符串
参见“遍历容器”。
操作字符串
QStringList 提供了若干函数,允许您对列表的内容进行操作。您可以使用 `join()` 函数将字符串列表中的所有字符串连接成一个字符串(可选分隔符)。例如:
QString str = fonts.join(", ");
// str == "Arial, Helvetica, Times, Courier"join 函数的参数可以是一个字符或一个字符串。
要将字符串拆分为字符串列表,请使用 `QString::split()` 函数:
QStringList list;
list = str.split(',');
// list: ["Arial", "Helvetica", "Times", "Courier"]split 函数的参数可以是单个字符、字符串或 `QRegularExpression`。
此外,operator+() 函数允许您将两个字符串列表拼接成一个。要对字符串列表进行排序,请使用sort() 函数。
QString list 还提供了 `filter()` 函数,它允许您提取一个新列表,该列表仅包含那些包含特定子字符串(或匹配特定正则表达式)的字符串:
QStringList monospacedFonts = fonts.filter(QRegularExpression("Courier|Fixed"));contains() 函数用于判断列表中是否包含给定字符串,而indexOf() 函数则返回该字符串首次出现的索引位置。另一方面,lastIndexOf() 函数则返回该字符串最后一次出现的索引位置。
最后,replaceInStrings() 函数会依次对字符串列表中的每个字符串调用QString::replace() 函数。例如:
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", ...]另请参阅 QString 。
成员函数文档
QStringList::QStringList(const QList<QString> &other)
创建other 的副本。
此操作耗时为常数时间,因为 QStringList 是隐式共享的。这使得从函数中返回一个 QStringList 非常快。如果共享的实例被修改,它将被复制(写时复制),而该操作耗时为线性时间。
另请参阅 operator=()。
QStringList::QStringList(const QString &str)
构建一个包含给定字符串str 的字符串列表。更长的列表可以像这样轻松创建:
QStringList longerList = (QStringList() << str1 << str2 << str3);另请参阅 append()。
QStringList::QStringList(QList<QString> &&other)
构建成功后,other 将为空。
这是一个重载函数。
[noexcept] bool QStringList::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
如果列表中包含字符串str ,则返回true ;否则返回false 。
如果cs 为Qt::CaseSensitive (默认值),则字符串比较区分大小写;否则,比较不区分大小写。
另请参阅 indexOf()、lastIndexOf() 和QString::contains()。
[noexcept] bool QStringList::contains(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
如果列表中包含由 `str` 解析的 Latin-1 字符串,则返回 `true `;否则返回 `false`。
如果cs 为Qt::CaseSensitive (默认值),则字符串比较区分大小写;否则,比较不区分大小写。
这是一个重载函数。
另请参阅 indexOf()、lastIndexOf() 和QString::contains()。
[noexcept] bool QStringList::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
如果列表中包含字符串str ,则返回true ;否则返回false 。
如果cs 为Qt::CaseSensitive (默认值),则字符串比较区分大小写;否则,比较不区分大小写。
这是一个重载函数。
[since 6.9] QStringList QStringList::filter(const QLatin1StringMatcher &matcher) const
返回一个列表,其中包含所有与 `matcher ` 匹配的字符串(即对于这些字符串,`matcher.indexIn() ` 返回的索引 >= 0)。
在大型列表和/或包含长字符串的列表中进行搜索时,使用QLatin1StringMatcher 可能会更快(最好的验证方法是进行基准测试)。
例如:
QStringList veryLargeList;
QLatin1StringMatcher matcher("Street"_L1, Qt::CaseInsensitive);
QStringList filtered = veryLargeList.filter(matcher);该函数于 Qt 6.9 中引入。
另请参阅 contains() 和filter(const QStringMatcher &)。
QStringList QStringList::filter(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
返回一个包含所有包含子字符串str 的字符串的列表。
如果cs 为Qt::CaseSensitive (默认值),则字符串比较区分大小写;否则,比较不区分大小写。
QStringList list;
list << "Bill Murray" << "John Doe" << "Bill Clinton";
QStringList result;
result = list.filter("Bill");
// result: ["Bill Murray", "Bill Clinton"]这等同于
QStringList result;
for (const auto &str : std::as_const(list)) {
if (str.contains("Bill"))
result += str;
}另请参阅 contains()。
QStringList QStringList::filter(const QRegularExpression &re) const
返回一个包含所有匹配正则表达式re 的字符串的列表。
这是一个重载函数。
[since 6.7] QStringList QStringList::filter(const QStringMatcher &matcher) const
返回一个包含所有与 `matcher ` 匹配的字符串的列表(即对于这些字符串,`matcher.indexIn() ` 返回的索引值大于等于 0)。
在大型列表和/或包含长字符串的列表中进行搜索时,使用QStringMatcher 可能会更快(最佳验证方法是进行基准测试)。
例如:
QStringList veryLongList;
QStringMatcher matcher(u"Straße", Qt::CaseInsensitive);
QStringList filtered = veryLongList.filter(matcher);这是一个重载函数。
该函数于 Qt 6.7 中引入。
另请参阅 contains() 和filter(const QLatin1StringMatcher &)。
[since 6.7] QStringList QStringList::filter(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
这是一个重载函数。
该函数在 Qt 6.7 中引入。
QStringList QStringList::filter(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
这是一个重载函数。
[noexcept] qsizetype QStringList::indexOf(QLatin1StringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
[noexcept] qsizetype QStringList::indexOf(QStringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
[noexcept] qsizetype QStringList::indexOf(const QString &str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
返回列表中第一个与 `str ` 匹配项的索引位置,从索引位置 `from` 开始向前搜索。如果没有匹配项,则返回 -1。
如果 `cs ` 为 `Qt::CaseSensitive `(默认值),则字符串比较区分大小写;否则,比较不区分大小写。
注意: cs 参数是在 Qt 6.7 中添加的,也就是说,这些方法现在重载了从基类继承的方法。在此之前,这些方法只有两个参数。此更改在源代码层面兼容,现有代码应能继续正常运行。
另请参阅 lastIndexOf()。
qsizetype QStringList::indexOf(const QRegularExpression &re, qsizetype from = 0) const
返回列表中第一个与re 完全匹配的项的索引位置,从索引位置from 开始向前搜索。若无匹配项,则返回-1。
这是一个重载函数。
另请参阅 lastIndexOf()。
QString QStringList::join(const QString &separator) const
将字符串列表中的所有字符串连接成一个字符串,每个元素之间用给定的separator 分隔(该分隔符可以是空字符串)。
另请参阅 QString::split()。
QString QStringList::join(QChar separator) const
该函数重载了QStringList::join()。
QString QStringList::join(QLatin1StringView separator) const
该函数重载了QStringList::join()。
QString QStringList::join(QStringView separator) const
这是一个重载函数。
[noexcept] qsizetype QStringList::lastIndexOf(QLatin1StringView str, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
[noexcept] qsizetype QStringList::lastIndexOf(QStringView str, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
[noexcept] qsizetype QStringList::lastIndexOf(const QString &str, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
返回列表中str 的最后一个匹配项的索引位置,从索引位置from 开始向后搜索。如果from 为-1(默认值),则从最后一个项开始搜索。若未找到匹配项,则返回-1。
如果 `cs ` 为 `Qt::CaseSensitive `(默认值),则字符串比较区分大小写;否则,比较不区分大小写。
注意: cs 参数是在 Qt 6.7 中新增的 ,即这些方法现在重载了从基类继承的方法。在此之前,这些方法只有两个参数。此更改在源代码层面兼容,现有代码应能继续正常运行。
另请参阅 indexOf()。
qsizetype QStringList::lastIndexOf(const QRegularExpression &re, qsizetype from = -1) const
返回列表中re 的最后一个精确匹配项的索引位置,从索引位置from 开始向后搜索。如果from 为-1(默认值),则从最后一个元素开始搜索。如果没有匹配项,则返回-1。
这是一个重载函数。
另请参阅 indexOf()。
qsizetype QStringList::removeDuplicates()
该函数用于从列表中删除重复项。列表中的条目无需排序,它们将保留原有的顺序。
返回已删除条目的数量。
QStringList &QStringList::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
返回一个字符串列表,其中每个字符串中,凡是出现before 文本之处,before 文本均已被替换为after 文本。
注意:如果 使用空的 `before ` 参数,则 `after ` 参数将被插入到字符串中每个字符的前后。
如果cs 为Qt::CaseSensitive (默认值),则字符串比较区分大小写;否则,比较不区分大小写。
例如:
QStringList list;
list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings("a", "o");
// list == ["olpho", "beto", "gommo", "epsilon"]另请参阅 QString::replace()。
QStringList &QStringList::replaceInStrings(const QRegularExpression &re, const QString &after)
将字符串列表中每个字符串内所有匹配正则表达式re 的字符串,替换为after 。返回该字符串列表的引用。
例如:
QStringList list;
list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings(QRegularExpression("^a"), "o");
// list == ["olpha", "beta", "gamma", "epsilon"]对于包含捕获组的正则表达式, \1, \2after, ..., 将分别替换为由相应捕获组捕获的字符串。
例如:
QStringList list;
list << "Bill Clinton" << "Murray, Bill";
list.replaceInStrings(QRegularExpression("^(.*), (.*)$"), "\\2 \\1");
// list == ["Bill Clinton", "Bill Murray"]这是一个重载函数。
QStringList &QStringList::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
这是一个重载函数。
QStringList &QStringList::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
这是一个重载函数。
QStringList &QStringList::replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
这是一个重载函数。
void QStringList::sort(Qt::CaseSensitivity cs = Qt::CaseSensitive)
将字符串列表按升序排序。
如果 `cs ` 的值为 `Qt::CaseSensitive `(默认值),则字符串比较区分大小写;否则,比较不区分大小写。
排序使用 STL 的 std::sort() 算法进行,其平均时间复杂度为线性-对数级,即 O(nlogn)。
如果您想按任意顺序对字符串进行排序,请考虑使用QMap 类。 例如,您可以使用QMap<QString,QString> 来创建不区分大小写的排序(例如,键为字符串的小写形式,值为字符串),或者使用QMap<int,QString> 根据某个整数索引对字符串进行排序。
QStringList QStringList::operator+(const QStringList &other) const
返回一个字符串列表,该列表由本字符串列表与other 字符串列表拼接而成。
另请参阅 append()。
QStringList &QStringList::operator<<(const QString &str)
将给定的字符串str 追加到此字符串列表中,并返回该字符串列表的引用。
另请参阅 append()。
QStringList &QStringList::operator<<(const QList<QString> &other)
将字符串列表 `other ` 追加到该字符串列表中,并返回对后者字符串列表的引用。
这是一个重载函数。
QStringList &QStringList::operator<<(const QStringList &other)
将字符串列表 `other ` 追加到该字符串列表中,并返回后者字符串列表的引用。
这是一个重载函数。
QStringList &QStringList::operator=(const QList<QString> &other)
从QList<QString> 复制赋值运算符。将字符串列表other 赋值给此字符串列表。
操作完成后,other 和*this 将相等。
QStringList &QStringList::operator=(QList<QString> &&other)
将赋值运算符从QList 移至<QString>。将other 中的字符串列表移动到此字符串列表中。
操作完成后,other 将变为空。
这是一个重载函数。
相关的非成员函数
[alias] QMutableStringListIterator
QStringListIterator 类型定义为QStringList 提供了一个 Java 风格的非 const 迭代器。
QStringList 同时提供了Java 风格的迭代器和 STL 风格的迭代器。Java 风格的非 const 迭代器仅仅是QMutableListIterator<QString> 的类型定义。
另请参阅 QStringListIterator 和QStringList::iterator 。
[alias] QStringListIterator
QStringListIterator 类型定义为QStringList 提供了一个 Java 风格的 const 迭代器。
QStringList 同时提供了Java 风格的迭代器和 STL 风格的迭代器。Java 风格的 const 迭代器仅仅是QListIterator<QString> 的类型定义。
另请参见 QMutableStringListIterator 和QStringList::const_iterator 。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.