QStringList Class

QStringList 类提供字符串列表。 更多...

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

注意: 此类的所有函数 reentrant .

公共函数

QStringList (const QString & str )
QStringList (const QList<QString> & other )
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
QStringList filter (const QString & str , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QStringList filter (QStringView str , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QStringList filter (const QRegularExpression & re ) const
qsizetype indexOf (const QRegularExpression & re , qsizetype from = 0) const
QString join (const QString & separator ) const
QString join (QStringView separator ) const
QString join (QLatin1StringView separator ) const
QString join (QChar separator ) 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 (QStringView before , QStringView after , Qt::CaseSensitivity cs = Qt::CaseSensitive)
QStringList & replaceInStrings (const QString & before , QStringView after , Qt::CaseSensitivity cs = Qt::CaseSensitive)
QStringList & replaceInStrings (QStringView before , const QString & after , Qt::CaseSensitivity cs = Qt::CaseSensitive)
QStringList & replaceInStrings (const QRegularExpression & re , const QString & after )
void sort (Qt::CaseSensitivity cs = Qt::CaseSensitive)
QStringList operator+ (const QStringList & other ) const
QStringList & operator<< (const QString & str )
QStringList & operator<< (const QStringList & other )
QStringList & operator<< (const QList<QString> & other )
QStringList & operator= (const QList<QString> & other )
QStringList & operator= (QList<QString> && other )
QMutableStringListIterator
QStringListIterator

详细描述

QStringList 继承自 QList < QString >。像 QList , QStringList is implicitly shared . It provides fast index-based access as well as fast insertions and removals. Passing string lists as value parameters is both fast and safe.

All of QList 's functionality also applies to QStringList. For example, you can use isEmpty () to test whether the list is empty, and you can call functions like append (), prepend (), insert (), replace (), removeAll (), removeAt (), removeFirst (), removeLast (),和 removeOne () to modify a QStringList. In addition, QStringList provides a few convenience functions that make handling lists of strings easier:

初始化

The default constructor creates an empty list. You can use the initializer-list constructor to create a list with elements:

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

添加字符串

Strings can be added to a list using the insert (), append (), operator+= () 和 operator<< () 函数。

operator<< () can be used to conveniently add multiple elements to a list:

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

遍历字符串

Iterating over Containers .

操纵字符串

QStringList provides several functions allowing you to manipulate the contents of a list. You can concatenate all the strings in a string list into a single string (with an optional separator) using the join () 函数。例如:

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

The argument to join can be a single character or a string.

To break up a string into a string list, use the QString::split () 函数:

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

The argument to split can be a single character, a string or a QRegularExpression .

此外, operator+ () function allows you to concatenate two string lists into one. To sort a string list, use the sort () 函数。

QString 列表还提供 filter () function which lets you to extract a new list which contains only those strings which contain a particular substring (or match a particular regular expression):

    QStringList monospacedFonts = fonts.filter(QRegularExpression("Courier|Fixed"));

The contains () function tells you whether the list contains a given string, while the indexOf () function returns the index of the first occurrence of the given string. The lastIndexOf () function on the other hand, returns the index of the last occurrence of the string.

Finally, the replaceInStrings () 函数调用 QString::replace () on each string in the string list in turn. For example:

    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 QString & str )

Constructs a string list that contains the given string, str . Longer lists are easily created like this:

    QStringList longerList = (QStringList() << str1 << str2 << str3);

另请参阅 append ().

QStringList:: QStringList (const QList < QString > & other )

构造副本为 other .

此操作花费 constant time , because QStringList is implicitly shared . This makes returning a QStringList from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes linear time .

另请参阅 operator= ().

版权所有  © 2014-2024 乐数软件    

工业和信息化部: 粤ICP备14079481号-1