QStringList Class

QStringList 类提供了一个字符串列表。更多

Header: #include <QStringList>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
继承: QList

注意:该类中的所有函数都是可重入的

公共函数

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<QString>。与QList 一样,QStringList 是隐式共享的。它提供基于索引的快速访问以及快速插入和删除。将字符串列表作为值参数传递既快速又安全。

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

初始化

默认构造函数会创建一个空列表。你可以使用 initializer-list 构造函数创建一个包含元素的列表:

    QStringList fonts = { "Arial", "Helvetica", "Times" };

添加字符串

可以使用insert(),append(),operator+=() 和operator<<() 函数将字符串添加到列表中。

operator<<() 函数可以方便地向列表中添加多个元素:

    fonts << "Courier" << "Verdana";

遍历字符串

请参阅对容器进行迭代

操作字符串

QStringList 提供了多个函数,允许你操作列表的内容。您可以使用join() 函数将字符串列表中的所有字符串连接成一个字符串(可选分隔符)。例如

    QString str = fonts.join(", ");
     // str == "Arial, Helvetica, Times, Courier"

要连接的参数可以是单个字符或字符串。

要将一个字符串分割成一个字符串列表,请使用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

成员函数文档

[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

从索引位置from 开始向前搜索,返回列表中str 第一个匹配项的索引位置。如果没有匹配项,则返回-1。

如果csQt::CaseSensitive (默认值),则字符串比较区分大小写;否则比较不区分大小写。

注: cs 参数是在 Qt XML 6.7 中添加的,即现在这些方法重载了从基类继承的方法。在此之前,这些方法只有两个参数。此更改与源代码兼容,现有代码应能继续工作。

另请参阅 lastIndexOf()。

[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

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

如果csQt::CaseSensitive (默认值),则字符串比较区分大小写;否则比较不区分大小写。

注: cs 参数是在 Qt XML 6.7 中添加的,即现在这些方法重载了从基类继承的方法。在此之前,这些方法只有两个参数。此更改与源代码兼容,现有代码应能继续工作。

另请参阅 indexOf()。

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)

这是一个重载函数。

Move-construct 从QList<QString> 开始。

成功构建后,other 将为空。

[noexcept] bool QStringList::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

如果列表包含字符串str ,则返回true ;否则返回false

如果csQt::CaseSensitive (默认值),则字符串比较区分大小写;否则比较不区分大小写。

另请参阅 indexOf()、lastIndexOf() 和QString::contains()。

[noexcept] bool QStringList::contains(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

这是一个重载函数。

如果列表包含str 查看的 Latin-1 字符串,则返回true ;否则返回false

如果csQt::CaseSensitive (默认值),则字符串比较区分大小写;否则比较不区分大小写。

另请参阅 indexOf()、lastIndexOf() 和QString::contains()。

[noexcept] bool QStringList::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

这是一个重载函数。

如果列表包含字符串str ,则返回true ;否则返回false

如果csQt::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 的所有字符串的列表。

如果csQt::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

这是一个重载函数。

qsizetype QStringList::indexOf(const QRegularExpression &re, qsizetype from = 0) const

这是一个重载函数。

从索引位置from 开始向前搜索,返回列表中re 第一个完全匹配项的索引位置。如果没有匹配项,则返回-1。

另请参见 lastIndexOf()。

QString QStringList::join(const QString &separator) const

将字符串列表中的所有字符串合并为一个字符串,每个元素之间用给定的separator (可以是空字符串)分隔。

另请参见 QString::split()。

QString QStringList::join(QChar separator) const

此函数重载 join()。

QString QStringList::join(QLatin1StringView separator) const

此函数重载 join()。

QString QStringList::join(QStringView separator) const

这是一个重载函数。

qsizetype QStringList::lastIndexOf(const QRegularExpression &re, qsizetype from = -1) const

这是一个重载函数。

从索引位置from 开始向后搜索,返回re 在列表中最后一个完全匹配项的索引位置。如果from 为 -1(默认值),则从最后一项开始搜索。如果没有匹配项,则返回-1。

另请参见 indexOf()。

qsizetype QStringList::removeDuplicates()

此函数用于删除列表中的重复条目。条目无需排序。它们将保留原来的顺序。

返回删除条目的数量。

QStringList &QStringList::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)

返回一个字符串列表,其中每个字符串中的before 文本都已替换为after 文本,只要找到before 文本。

注意: 如果使用空before 参数,将在字符串的每个字符前后插入after 参数。

如果csQt::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)

这是一个重载函数。

after 替换正则表达式re 在字符串列表的每个字符串中的每一次出现。返回字符串列表的引用。

例如

    QStringList list;
    list << "alpha" << "beta" << "gamma" << "epsilon";
    list.replaceInStrings(QRegularExpression("^a"), "o");
    // list == ["olpha", "beta", "gamma", "epsilon"]

对于包含捕获组的正则表达式,after 中出现的\1,\2, ...,将被替换为相应捕获组捕获的字符串。

例如

    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)

按升序对字符串列表排序。

如果csQt::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 风格的非共存迭代器。

QStringList迭代器类型定义中,我们同时提供了Java 风格的 迭代器STL 风格的迭代器。Java 样式的非const迭代器只是 < > 的一个类型定义。QMutableListIteratorQString

另请参见 QStringListIteratorQStringList::iterator

[alias] QStringListIterator

QStringListIterator 类型定义为QStringList 提供了 Java 风格的常量迭代器。

QStringList QStringListIterator 提供了Java 风格的迭代器STL 风格的迭代器。Java 风格的常量迭代器只是 < > 的一个类型定义。QListIteratorQString

另请参见 QMutableStringListIteratorQStringList::const_iterator

© 2025 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.