QStringList 類提供字符串列錶。 更多...
| 頭: |
#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 |
| QStringList | filter (const QString & str , Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| QStringList | filter (const QRegularExpression & re ) const |
(從 6.7 起)
QStringList
|
filter (const QStringMatcher & matcher ) const |
(從 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 ) |
| QMutableStringListIterator | |
| QStringListIterator |
QStringList 繼承自 QList < QString >。像 QList ,QStringList 為 隱式共享 。它提供快速基於索引的訪問,及快速插入和移除。以值參數形式傳遞字符串列錶,既快又安全。
所有的 QList 的功能也適用於 QStringList。例如,可以使用 isEmpty () 來測試列錶是否為空,可以調用函數像 append (), prepend (), insert (), replace (), removeAll (), removeAt (), removeFirst (), removeLast (),和 removeOne () 來修改 QStringList。此外,QStringList 提供瞭幾個方便函數,使處理字符串列錶變得更輕鬆。
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";
見 遍曆容器 .
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 .
[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
Returns the index position of the first match of str in the list, searching forward from index position from . Returns -1 if no item matched.
若 cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.
注意: The cs parameter was added in Qt 6.7, i.e. these methods now overload the methods inherited from the base class. Prior to that these methods only had two parameters. This change is source compatible and existing code should continue to work.
另請參閱 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
Returns the index position of the last match of str in the list, searching backward from index position from 。若 from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.
若 cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.
注意: The cs parameter was added in Qt 6.7, i.e. these methods now overload the methods inherited from the base class. Prior to that these methods only had two parameters. This change is source compatible and existing code should continue to work.
另請參閱 indexOf ().
構造副本為 other .
此操作花費 常量時間 , because QStringList is 隱式共享 . 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 綫性時間 .
另請參閱 operator= ().
Constructs a string list that contains the given string, str . Longer lists are easily created like this:
QStringList longerList = (QStringList() << str1 << str2 << str3);
另請參閱 append ().
這是重載函數。
Move-constructs from QList < QString >.
After a successful construction, other will be empty.
[noexcept]
bool
QStringList::
contains
(const
QString
&
str
,
Qt::CaseSensitivity
cs
= Qt::CaseSensitive) const
返迴
true
若列錶包含字符串
str
;否則返迴
false
.
若 cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.
另請參閱 indexOf (), lastIndexOf (),和 QString::contains ().
[noexcept]
bool
QStringList::
contains
(
QLatin1StringView
str
,
Qt::CaseSensitivity
cs
= Qt::CaseSensitive) const
這是重載函數。
返迴
true
if the list contains the Latin-1 string viewed by
str
;否則返迴
false
.
若 cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.
另請參閱 indexOf (), lastIndexOf (),和 QString::contains ().
[noexcept]
bool
QStringList::
contains
(
QStringView
str
,
Qt::CaseSensitivity
cs
= Qt::CaseSensitive) const
這是重載函數。
返迴
true
若列錶包含字符串
str
;否則返迴
false
.
若 cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.
返迴所有字符串的列錶包含子字符串 str .
若 cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.
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 ().
這是重載函數。
Returns a list of all the strings that match the regular expression re .
[since 6.7]
QStringList
QStringList::
filter
(const
QStringMatcher
&
matcher
) const
這是重載函數。
Returns a list of all the strings matched by
matcher
(i.e. for which
matcher.indexIn()
returns an index >= 0).
使用 QStringMatcher may be faster when searching in large lists and/or in lists with long strings (the best way to find out is benchmarking).
例如:
QStringList veryLongList;
QStringMatcher matcher(u"Straße", Qt::CaseInsensitive);
QStringList filtered = veryLongList.filter(matcher);
該函數在 Qt 6.7 引入。
另請參閱 contains ().
[since 6.7]
QStringList
QStringList::
filter
(
QLatin1StringView
str
,
Qt::CaseSensitivity
cs
= Qt::CaseSensitive) const
這是重載函數。
該函數在 Qt 6.7 引入。
這是重載函數。
這是重載函數。
Returns the index position of the first exact match of re in the list, searching forward from index position from . Returns -1 if no item matched.
另請參閱 lastIndexOf ().
Joins all the string list's strings into a single string with each element separated by the given separator (which can be an empty string).
另請參閱 QString::split ().
This function overloads join().
This function overloads join().
這是重載函數。
這是重載函數。
Returns the index position of the last exact match of re in the list, searching backward from index position from 。若 from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.
另請參閱 indexOf ().
This function removes duplicate entries from a list. The entries do not have to be sorted. They will retain their original order.
Returns the number of removed entries.
Returns a string list where every string has had the before text replaced with the after text wherever the before text is found.
注意: If you use an empty before argument, the after argument will be inserted before and after each character of the string.
若 cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.
例如:
QStringList list;
list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings("a", "o");
// list == ["olpho", "beto", "gommo", "epsilon"]
另請參閱 QString::replace ().
這是重載函數。
Replaces every occurrence of the regular expression re , in each of the string lists's strings, with after . Returns a reference to the string list.
例如:
QStringList list;
list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings(QRegularExpression("^a"), "o");
// list == ["olpha", "beta", "gamma", "epsilon"]
For regular expressions that contain capturing groups, occurrences of \1 , \2 , ..., in after are replaced with the string captured by the corresponding capturing group.
例如:
QStringList list;
list << "Bill Clinton" << "Murray, Bill";
list.replaceInStrings(QRegularExpression("^(.*), (.*)$"), "\\2 \\1");
// list == ["Bill Clinton", "Bill Murray"]
這是重載函數。
這是重載函數。
這是重載函數。
Sorts the list of strings in ascending order.
若 cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.
Sorting is performed using the STL's std::sort() algorithm, which averages linear-logarithmic time , i.e. O( n log n ).
If you want to sort your strings in an arbitrary order, consider using the QMap class. For example, you could use a QMap < QString , QString > to create a case-insensitive ordering (e.g. with the keys being lower-case versions of the strings, and the values being the strings), or a QMap <int, QString > to sort the strings by some integer index.
Returns a string list that is the concatenation of this string list with the other string list.
另請參閱 append ().
Appends the given string, str , to this string list and returns a reference to the string list.
另請參閱 append ().
這是重載函數。
追加 other string list to the string list and returns a reference to the latter string list.
這是重載函數。
追加 other string list to the string list and returns a reference to the latter string list.
Copy assignment operator from QList < QString >. Assigns the other list of strings to this string list.
After the operation,
other
and
*this
will be equal.
這是重載函數。
Move assignment operator from QList < QString >. Moves the other list of strings to this string list.
After the operation, other will be empty.
[alias]
QMutableStringListIterator
The QStringListIterator type definition provides a Java-style non-const iterator for QStringList .
QStringList provides both Java 風格迭代器 and STL 樣式迭代器 . The Java-style non-const iterator is simply a type definition for QMutableListIterator < QString >.
另請參閱 QStringListIterator and QStringList::iterator .
[alias]
QStringListIterator
The QStringListIterator type definition provides a Java-style const iterator for QStringList .
QStringList provides both Java 風格迭代器 and STL 樣式迭代器 . The Java-style const iterator is simply a type definition for QListIterator < QString >.
另請參閱 QMutableStringListIterator and QStringList::const_iterator .