The QStringView class provides a unified view on UTF-16 strings with a read-only subset of the QString API. More...
Header: | #include <QStringView> |
qmake: | QT += core |
Since: | Qt 5.10 |
This class was introduced in Qt 5.10.
Note: All functions in this class are reentrant.
typedef | const_iterator |
typedef | const_pointer |
typedef | const_reference |
typedef | const_reverse_iterator |
typedef | difference_type |
typedef | iterator |
typedef | pointer |
typedef | reference |
typedef | reverse_iterator |
typedef | size_type |
typedef | storage_type |
typedef | value_type |
QStringView(const StdBasicString &str) | |
QStringView(const QStringRef &str) | |
QStringView(const QString &str) | |
QStringView(const Char *str) | |
QStringView(const Char (&)[N] string = N) | |
QStringView(const Char *first, const Char *last) | |
QStringView(const Char *str, qsizetype len) | |
QStringView(std::nullptr_t) | |
QStringView() | |
QString | arg(Args &&... args) const |
QChar | at(qsizetype n) const |
QChar | back() const |
QStringView::const_iterator | begin() const |
QStringView::const_iterator | cbegin() const |
QStringView::const_iterator | cend() const |
void | chop(qsizetype length) |
QStringView | chopped(qsizetype length) const |
int | compare(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | compare(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | compare(QChar ch) const |
int | compare(QChar ch, Qt::CaseSensitivity cs) const |
bool | contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | contains(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QStringView::const_reverse_iterator | crbegin() const |
QStringView::const_reverse_iterator | crend() const |
QStringView::const_pointer | data() const |
bool | empty() const |
QStringView::const_iterator | end() const |
bool | endsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | endsWith(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | endsWith(QChar ch) const |
bool | endsWith(QChar ch, Qt::CaseSensitivity cs) const |
QChar | first() const |
QChar | front() const |
qsizetype | indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
qsizetype | indexOf(QStringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
qsizetype | indexOf(QLatin1String l1, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | isEmpty() const |
bool | isNull() const |
bool | isRightToLeft() const |
bool | isValidUtf16() const |
QChar | last() const |
qsizetype | lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
qsizetype | lastIndexOf(QStringView str, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
qsizetype | lastIndexOf(QLatin1String l1, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QStringView | left(qsizetype length) const |
int | length() const |
QStringView | mid(qsizetype start) const |
QStringView | mid(qsizetype start, qsizetype length) const |
QStringView::const_reverse_iterator | rbegin() const |
QStringView::const_reverse_iterator | rend() const |
QStringView | right(qsizetype length) const |
qsizetype | size() const |
bool | startsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | startsWith(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | startsWith(QChar ch) const |
bool | startsWith(QChar ch, Qt::CaseSensitivity cs) const |
QByteArray | toLatin1() const |
QByteArray | toLocal8Bit() const |
QString | toString() const |
QVector<uint> | toUcs4() const |
QByteArray | toUtf8() const |
int | toWCharArray(wchar_t *array) const |
QStringView | trimmed() const |
void | truncate(qsizetype length) |
const QStringView::storage_type * | utf16() const |
QChar | operator[](qsizetype n) const |
uint | qHash(QStringView key, uint seed = 0) |
A QStringView references a contiguous portion of a UTF-16 string it does not own. It acts as an interface type to all kinds of UTF-16 string, without the need to construct a QString first.
The UTF-16 string may be represented as an array (or an array-compatible data-structure such as QString, std::basic_string, etc.) of QChar, ushort
, char16_t
or (on platforms, such as Windows, where it is a 16-bit type) wchar_t
.
QStringView is designed as an interface type; its main use-case is as a function parameter type. When QStringViews are used as automatic variables or data members, care must be taken to ensure that the referenced string data (for example, owned by a QString) outlives the QStringView on all code paths, lest the string view ends up referencing deleted data.
When used as an interface type, QStringView allows a single function to accept a wide variety of UTF-16 string data sources. One function accepting QStringView thus replaces three function overloads (taking QString, QStringRef, and (const QChar*, int)
), while at the same time enabling even more string data sources to be passed to the function, such as u"Hello World"
, a char16_t
string literal.
QStringViews should be passed by value, not by reference-to-const:
void myfun1(QStringView sv); // preferred void myfun2(const QStringView &sv); // compiles and works, but slower
If you want to give your users maximum freedom in what strings they can pass to your function, accompany the QStringView overload with overloads for
void fun(QChar ch) { fun(QStringView(&ch, 1)); }
even though, for technical reasons, QStringView cannot provide a QChar constructor by itself.
QStringView can also be used as the return value of a function. If you call a function returning QStringView, take extra care to not keep the QStringView around longer than the function promises to keep the referenced string data alive. If in doubt, obtain a strong reference to the data by calling toString() to convert the QStringView into a QString.
QStringView is a Literal Type, but since it stores data as char16_t
, iteration is not constexpr
(casts from const char16_t*
to const QChar*
, which is not allowed in constexpr
functions). You can use an indexed loop and/or utf16() in constexpr
contexts instead.
Note: We strongly discourage the use of QList<QStringView>, because QList is a very inefficient container for QStringViews (it would heap-allocate every element). Use QVector (or std::vector) to hold QStringViews instead.
See also QString and QStringRef.
This typedef provides an STL-style const iterator for QStringView.
See also iterator and const_reverse_iterator.
Alias for value_type *
. Provided for compatibility with the STL.
Alias for value_type &
. Provided for compatibility with the STL.
This typedef provides an STL-style const reverse iterator for QStringView.
See also reverse_iterator and const_iterator.
Alias for std::ptrdiff_t
. Provided for compatibility with the STL.
This typedef provides an STL-style const iterator for QStringView.
QStringView does not support mutable iterators, so this is the same as const_iterator.
See also const_iterator and reverse_iterator.
Alias for value_type *
. Provided for compatibility with the STL.
QStringView does not support mutable pointers, so this is the same as const_pointer.
Alias for value_type &
. Provided for compatibility with the STL.
QStringView does not support mutable references, so this is the same as const_reference.
This typedef provides an STL-style const reverse iterator for QStringView.
QStringView does not support mutable reverse iterators, so this is the same as const_reverse_iterator.
See also const_reverse_iterator and iterator.
Alias for qsizetype. Provided for compatibility with the STL.
Unlike other Qt classes, QStringView uses qsizetype as its size_type
, to allow accepting data from std::basic_string
without truncation. The Qt API functions, for example length(), return int
, while the STL-compatible functions, for example size(), return size_type
.
Alias for char16_t
.
Alias for const QChar
. Provided for compatibility with the STL.
Returns the index position of the last occurrence of the string-view str, Latin-1 string l1, or character ch, respectively, in this string-view, searching backward from index position from. If from is -1 (default), the search starts at the last character; if from is -2, at the next to last character and so on. Returns -1 if str is not found.
If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.
This function was introduced in Qt 5.14.
See also QString::lastIndexOf().
Returns true
if this string-view contains an occurrence of the string-view str, Latin-1 string l1, or character ch; otherwise returns false
.
If cs is Qt::CaseSensitive (the default), the search is case-sensitive; otherwise the search is case-insensitive.
This function was introduced in Qt 5.14.
See also indexOf().
Returns the index position of the first occurrence of the string-view str, Latin-1 string l1, or character ch, respectively, in this string-view, searching forward from index position from. Returns -1 if str is not found.
If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.
If from is -1, the search starts at the last character; if it is -2, at the next to last character and so on.
This function was introduced in Qt 5.14.
See also QString::indexOf().
Returns true
if this string-view ends with string-view str, Latin-1 string l1, or character ch, respectively; otherwise returns false
.
If cs is Qt::CaseSensitive (the default), the search is case-sensitive; otherwise the search is case-insensitive.
See also startsWith().
Returns true
if this string-view starts with string-view str, Latin-1 string l1, or character ch, respectively; otherwise returns false
.
If cs is Qt::CaseSensitive (the default), the search is case-sensitive; otherwise the search is case-insensitive.
See also endsWith().
Returns an integer that compares to zero as this string-view compares to the Latin-1 string l1, or character ch, respectively.
If cs is Qt::CaseSensitive (the default), the comparison is case sensitive; otherwise the comparison is case-insensitive.
This function was introduced in Qt 5.14.
See also operator==(), operator<(), and operator>().
Replaces occurrences of %N
in this string with the corresponding argument from args. The arguments are not positional: the first of the args replaces the %N
with the lowest N
(all of them), the second of the args the %N
with the next-lowest N
etc.
Args
can consist of anything that implicitly converts to QString, QStringView or QLatin1String.
In addition, the following types are also supported: QChar, QLatin1Char.
This function was introduced in Qt 5.14.
See also QString::arg().
Constructs a string view on str. The length is taken from str.size()
.
str.data()
must remain valid for the lifetime of this string view object.
This constructor only participates in overload resolution if StdBasicString
is an instantiation of std::basic_string
with a compatible character type. The compatible character types are: QChar
, ushort
, char16_t
and (on platforms, such as Windows, where it is a 16-bit type) wchar_t
.
The string view will be empty if and only if str.empty()
. It is unspecified whether this constructor can result in a null string view (str.data()
would have to return nullptr
for this).
See also isNull() and isEmpty().
Constructs a string view on str.
str.data()
must remain valid for the lifetime of this string view object.
The string view will be null if and only if str.isNull()
.
Constructs a string view on str.
str.data()
must remain valid for the lifetime of this string view object.
The string view will be null if and only if str.isNull()
.
Constructs a string view on str. The length is determined by scanning for the first Char(0)
.
str must remain valid for the lifetime of this string view object.
Passing nullptr
as str is safe and results in a null string view.
This constructor only participates in overload resolution if str is not an array and if Char
is a compatible character type. The compatible character types are: QChar
, ushort
, char16_t
and (on platforms, such as Windows, where it is a 16-bit type) wchar_t
.
Constructs a string view on the character string literal string. The length is set to N-1
, excluding the trailing {Char(0)}. If you need the full array, use the constructor from pointer and size instead:
auto sv = QStringView(array, std::size(array)); // using C++17 std::size()
string must remain valid for the lifetime of this string view object.
This constructor only participates in overload resolution if string is an actual array and Char
is a compatible character type. The compatible character types are: QChar
, ushort
, char16_t
and (on platforms, such as Windows, where it is a 16-bit type) wchar_t
.
Constructs a string view on first with length (last - first).
The range [first,last)
must remain valid for the lifetime of this string view object.
Passing \nullptr
as first is safe if last is nullptr
, too, and results in a null string view.
The behavior is undefined if last precedes first, or first is nullptr
and last is not.
This constructor only participates in overload resolution if Char
is a compatible character type. The compatible character types are: QChar
, ushort
, char16_t
and (on platforms, such as Windows, where it is a 16-bit type) wchar_t
.
Constructs a string view on str with length len.
The range [str,len)
must remain valid for the lifetime of this string view object.
Passing nullptr
as str is safe if len is 0, too, and results in a null string view.
The behavior is undefined if len is negative or, when positive, if str is nullptr
.
This constructor only participates in overload resolution if Char
is a compatible character type. The compatible character types are: QChar
, ushort
, char16_t
and (on platforms, such as Windows, where it is a 16-bit type) wchar_t
.
Constructs a null string view.
See also isNull().
Constructs a null string view.
See also isNull().
Returns the character at position n in this string view.
The behavior is undefined if n is negative or not less than size().
See also operator[](), front(), and back().
Returns the last character in the string. Same as last().
This function is provided for STL compatibility.
Warning: Calling this function on an empty string view constitutes undefined behavior.
See also front(), first(), and last().
Returns a const STL-style iterator pointing to the first character in the string.
This function is provided for STL compatibility.
See also end(), cbegin(), rbegin(), and data().
Same as begin().
This function is provided for STL compatibility.
See also cend(), begin(), crbegin(), and data().
Same as end().
This function is provided for STL compatibility.
See also cbegin(), end(), and crend().
Truncates this string view by length characters.
Same as *this = left(size() - length)
.
Note: The behavior is undefined when length < 0 or length > size().
See also mid(), left(), right(), chopped(), and truncate().
Returns the substring of length size() - length starting at the beginning of this object.
Same as left(size() - length)
.
Note: The behavior is undefined when length < 0 or length > size().
See also mid(), left(), right(), chop(), and truncate().
Returns an integer that compares to zero as this string-view compares to the string-view str.
If cs is Qt::CaseSensitive (the default), the comparison is case sensitive; otherwise the comparison is case-insensitive.
This function was introduced in Qt 5.12.
See also operator==(), operator<(), and operator>().
Same as rbegin().
This function is provided for STL compatibility.
See also crend(), rbegin(), and cbegin().
Same as rend().
This function is provided for STL compatibility.
See also crbegin(), rend(), and cend().
Returns a const pointer to the first character in the string.
Note: The character array represented by the return value is not null-terminated.
See also begin(), end(), and utf16().
Returns whether this string view is empty - that is, whether size() == 0
.
This function is provided for STL compatibility.
See also isEmpty(), isNull(), size(), and length().
Returns a const STL-style iterator pointing to the imaginary character after the last character in the list.
This function is provided for STL compatibility.
See also begin(), cend(), and rend().
Returns the first character in the string. Same as front().
This function is provided for compatibility with other Qt containers.
Warning: Calling this function on an empty string view constitutes undefined behavior.
See also front(), back(), and last().
Returns the first character in the string. Same as first().
This function is provided for STL compatibility.
Warning: Calling this function on an empty string view constitutes undefined behavior.
See also back(), first(), and last().
Returns whether this string view is empty - that is, whether size() == 0
.
This function is provided for compatibility with other Qt containers.
See also empty(), isNull(), size(), and length().
Returns whether this string view is null - that is, whether data() == nullptr
.
This functions is provided for compatibility with other Qt containers.
See also empty(), isEmpty(), size(), and length().
Returns true
if the string is read right to left.
This function was introduced in Qt 5.11.
See also QString::isRightToLeft().
Returns true
if the string contains valid UTF-16 encoded data, or false
otherwise.
Note that this function does not perform any special validation of the data; it merely checks if it can be successfully decoded from UTF-16. The data is assumed to be in host byte order; the presence of a BOM is meaningless.
This function was introduced in Qt 5.15.
See also QString::isValidUtf16().
Returns the last character in the string. Same as back().
This function is provided for compatibility with other Qt containers.
Warning: Calling this function on an empty string view constitutes undefined behavior.
See also back(), front(), and first().
Returns the substring of length length starting at position 0 in this object.
Note: The behavior is undefined when length < 0 or length > size().
See also mid(), right(), chopped(), chop(), and truncate().
Same as size(), except returns the result as an int
.
This function is provided for compatibility with other Qt containers.
Warning: QStringView can represent strings with more than 231 characters. Calling this function on a string view for which size() returns a value greater than INT_MAX
constitutes undefined behavior.
See also empty(), isEmpty(), isNull(), and size().
Returns the substring starting at position start in this object, and extending to the end of the string.
Note: The behavior is undefined when start < 0 or start > size().
See also left(), right(), chopped(), chop(), and truncate().
This is an overloaded function.
Returns the substring of length length starting at position start in this object.
Note: The behavior is undefined when start < 0, length < 0, or start + length > size().
See also left(), right(), chopped(), chop(), and truncate().
Returns a const STL-style reverse iterator pointing to the first character in the string, in reverse order.
This function is provided for STL compatibility.
See also rend(), crbegin(), and begin().
Returns a STL-style reverse iterator pointing to one past the last character in the string, in reverse order.
This function is provided for STL compatibility.
See also rbegin(), crend(), and end().
Returns the substring of length length starting at position size() - length in this object.
Note: The behavior is undefined when length < 0 or length > size().
See also mid(), left(), chopped(), chop(), and truncate().
Returns the size of this string view, in UTF-16 code points (that is, surrogate pairs count as two for the purposes of this function, the same as in QString and QStringRef).
See also empty(), isEmpty(), isNull(), and length().
Returns a Latin-1 representation of the string as a QByteArray.
The behavior is undefined if the string contains non-Latin1 characters.
See also toUtf8(), toLocal8Bit(), and QTextCodec.
Returns a local 8-bit representation of the string as a QByteArray.
QTextCodec::codecForLocale() is used to perform the conversion from Unicode. If the locale's encoding could not be determined, this function does the same as toLatin1().
The behavior is undefined if the string contains characters not supported by the locale's 8-bit encoding.
See also toLatin1(), toUtf8(), and QTextCodec.
Returns a deep copy of this string view's data as a QString.
The return value will be the null QString if and only if this string view is null.
Warning: QStringView can store strings with more than 230 characters while QString cannot. Calling this function on a string view for which size() returns a value greater than INT_MAX / 2
constitutes undefined behavior.
Returns a UCS-4/UTF-32 representation of the string as a QVector<uint>.
UCS-4 is a Unicode codec and therefore it is lossless. All characters from this string will be encoded in UCS-4. Any invalid sequence of code units in this string is replaced by the Unicode replacement character (QChar::ReplacementCharacter, which corresponds to U+FFFD
).
The returned vector is not 0-terminated.
See also toUtf8(), toLatin1(), toLocal8Bit(), and QTextCodec.
Returns a UTF-8 representation of the string as a QByteArray.
UTF-8 is a Unicode codec and can represent all characters in a Unicode string like QString.
See also toLatin1(), toLocal8Bit(), and QTextCodec.
Transcribes this string into the given array.
The caller is responsible for ensuring array is large enough to hold the wchar_t
encoding of this string (allocating the array with the same length as the string is always sufficient). The array is encoded in UTF-16 on platforms where wchar_t
is 2 bytes wide (e.g. Windows); otherwise (Unix systems), wchar_t
is assumed to be 4 bytes wide and the data is written in UCS-4.
Note: This function writes no null terminator to the end of array.
Returns the number of wchar_t
entries written to array.
This function was introduced in Qt 5.14.
See also QString::toWCharArray().
Strips leading and trailing whitespace and returns the result.
Whitespace means any character for which QChar::isSpace() returns true
. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.
Truncates this string view to length length.
Same as *this = left(length)
.
Note: The behavior is undefined when length < 0 or length > size().
See also mid(), left(), right(), chopped(), and chop().
Returns a const pointer to the first character in the string.
storage_type
is char16_t
.
Note: The character array represented by the return value is not null-terminated.
See also begin(), end(), and data().
Returns the character at position n in this string view.
The behavior is undefined if n is negative or not less than size().
See also at(), front(), and back().
Returns the hash value for the key, using seed to seed the calculation.
This function was introduced in Qt 5.10.
© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-5.15/qstringview.html