QStringView Class▲
-
Header: QStringView
-
Since: Qt 5.10
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Group: QStringView is part of tools, string-processing
Detailed Description▲
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 and (const QChar*, qsizetype)), 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 &
amp;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
-
QChar: this overload can delegate to the QStringView version:
Sélectionnezvoid
fun(QChar ch){
fun(QStringView(&
amp;ch,1
));}
even though, for technical reasons, QStringView cannot provide a QChar constructor by itself.
-
QString: if you store an unmodified copy of the string and thus would like to take advantage of QString's implicit sharing.
-
QLatin1StringView: if you can implement the function without converting the QLatin1StringView to UTF-16 first; users expect a function overloaded on QLatin1StringView to perform strictly less memory allocations than the semantically equivalent call of the QStringView version, involving construction of a QString from the QLatin1StringView.
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.
See Also▲
See also QString
Member Type Documentation▲
QStringView::const_iterator▲
This typedef provides an STL-style const iterator for QStringView.
See Also▲
See also iterator, const_reverse_iterator
QStringView::const_pointer▲
Alias for value_type *. Provided for compatibility with the STL.
QStringView::const_reference▲
Alias for value_type &. Provided for compatibility with the STL.
QStringView::const_reverse_iterator▲
This typedef provides an STL-style const reverse iterator for QStringView.
See Also▲
See also reverse_iterator, const_iterator
QStringView::difference_type▲
Alias for std::ptrdiff_t. Provided for compatibility with the STL.
QStringView::iterator▲
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▲
See also const_iterator, reverse_iterator
QStringView::pointer▲
Alias for value_type *. Provided for compatibility with the STL.
QStringView does not support mutable pointers, so this is the same as const_pointer.
QStringView::reference▲
Alias for value_type &. Provided for compatibility with the STL.
QStringView does not support mutable references, so this is the same as const_reference.
QStringView::reverse_iterator▲
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▲
See also const_reverse_iterator, iterator
QStringView::size_type▲
Alias for qsizetype. Provided for compatibility with the STL.
QStringView::storage_type▲
Alias for char16_t.
QStringView::value_type▲
Alias for const QChar. Provided for compatibility with the STL.
Member Function Documentation▲
[since 6.0] QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
[since 6.0] QList<QStringView> QStringView::split(QStringView sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Splits the view into substring views wherever sep occurs, and returns the list of those string views.
See QString::split() for how sep, behavior and cs interact to form the result.
All the returned views are valid as long as the data referenced by this string view is valid. Destroying the data will cause all views to become dangling.
This function was introduced in Qt 6.0.
[since 5.14] QString QStringView::arg(Args &&... args) const▲
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 QLatin1StringView.
In addition, the following types are also supported: QChar, QLatin1Char.
This function was introduced in Qt 5.14.
See Also▲
See also QString::arg()
[constexpr, since 5.15] int QStringView::compare(QChar ch) const▲
[since 5.15] int QStringView::compare(QChar ch, Qt::CaseSensitivity cs) const
[since 5.15] int QStringView::compare(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
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.15.
See Also▲
See also operator==(), operator<(), operator>()
bool QStringView::startsWith(QChar ch) const▲
bool QStringView::startsWith(QChar ch, Qt::CaseSensitivity cs) const
bool QStringView::startsWith(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool QStringView::startsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
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▲
See also endsWith()
bool QStringView::endsWith(QChar ch) const▲
bool QStringView::endsWith(QChar ch, Qt::CaseSensitivity cs) const
bool QStringView::endsWith(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool QStringView::endsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
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▲
See also startsWith()
[since 5.14] qsizetype QStringView::indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
[since 5.14] qsizetype QStringView::indexOf(QLatin1StringView l1, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
[since 5.14] qsizetype QStringView::indexOf(QStringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
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▲
See also QString::indexOf()
[since 5.14] bool QStringView::contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
[since 5.14] bool QStringView::contains(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
[since 5.14] bool QStringView::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
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▲
See also indexOf()
[since 5.14] qsizetype QStringView::lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
[since 5.14] qsizetype QStringView::lastIndexOf(QLatin1StringView l1, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
[since 5.14] qsizetype QStringView::lastIndexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
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, 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.
When searching for a 0-length str or l1, the match at the end of the data is excluded from the search by a negative from, even though -1 is normally thought of as searching from the end of the string view: the match at the end is after the last character, so it is excluded. To include such a final empty match, either give a positive value for from or omit the from parameter entirely.
This function was introduced in Qt 5.14.
See Also▲
See also QString::lastIndexOf()
[since 6.2] qsizetype QStringView::lastIndexOf(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
[since 6.2] qsizetype QStringView::lastIndexOf(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
This function overloads lastIndexOf().
Returns the index position of the last occurrence of the string view str or Latin-1 string l1, respectively, in this string view, searching backward from the last character of this string view. 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 6.2.
See Also▲
See also QString::lastIndexOf()
[constexpr, since 6.0] decltype(qTokenize(*this, std::forward<Needle>(needle), flags...)) QStringView::tokenize(Needle &&sep, Flags... flags) const▲
Splits the string into substring views wherever sep occurs, and returns a lazy sequence of those strings.
Equivalent to
return
QStringTokenizer{
std::
forward&
lt;Needle&
gt;(sep), flags...}
;
except it works without C++17 Class Template Argument Deduction (CTAD) enabled in the compiler.
See QStringTokenizer for how sep and flags interact to form the result.
While this function returns QStringTokenizer, you should never, ever, name its template arguments explicitly. If you can use C++17 Class Template Argument Deduction (CTAD), you may write
QStringTokenizer result =
sv.tokenize(sep);
(without template arguments). If you can't use C++17 CTAD, you must store the return value only in auto variables:
auto
result =
sv.tokenize(sep);
This is because the template arguments of QStringTokenizer have a very subtle dependency on the specific tokenize() overload from which they are returned, and they don't usually correspond to the type used for the separator.
This function was introduced in Qt 6.0.
See Also▲
See also QStringTokenizer, qTokenize()
[constexpr] QStringView::QStringView()▲
[constexpr] QStringView::QStringView(std::nullptr_t)▲
[constexpr] QStringView::QStringView(const Char *str, qsizetype len)▲
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.
[constexpr] QStringView::QStringView(const Char *first, const Char *last)▲
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.
[constexpr] QStringView::QStringView(const Char (&)[N] string = N)▲
Constructs a string view on the character string literal string. The view covers the array until the first Char(0) is encountered, or N, whichever comes first. If you need the full array, use fromArray() instead.
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.
See Also▲
See also fromArray
[constexpr] QStringView::QStringView(const Char *str)▲
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.
QStringView::QStringView(const QString &str)▲
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().
[constexpr] QStringView::QStringView(const Container &str)▲
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▲
[constexpr] QChar QStringView::at(qsizetype n) const▲
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▲
See also operator[](), front(), back()
[constexpr] QChar QStringView::back() const▲
Returns the last character in the string view. Same as last().
This function is provided for STL compatibility.
Calling this function on an empty string view constitutes undefined behavior.
See Also▲
QStringView::const_iterator QStringView::begin() const▲
Returns a const STL-style iterator pointing to the first character in the string view.
This function is provided for STL compatibility.
See Also▲
See also end(), constBegin(), cbegin(), rbegin(), data()
QStringView::const_iterator QStringView::cbegin() const▲
Same as begin().
This function is provided for STL compatibility.
See Also▲
See also cend(), begin(), constBegin(), crbegin(), data()
QStringView::const_iterator QStringView::cend() const▲
[constexpr] void QStringView::chop(qsizetype length)▲
Truncates this string view by length characters.
Same as *this = left(size() - length).
The behavior is undefined when length < 0 or length > size().
See Also▲
[constexpr] QStringView QStringView::chopped(qsizetype length) const▲
Returns the substring of length size() - length starting at the beginning of this object.
Same as left(size() - length).
The behavior is undefined when length < 0 or length > size().
See Also▲
[since 5.12] int QStringView::compare(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
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▲
See also operator==(), operator<(), operator>()
[since 6.1] QStringView::const_iterator QStringView::constBegin() const▲
[since 6.0] QStringView::const_pointer QStringView::constData() const▲
Returns a const pointer to the first character in the string view.
The character array represented by the return value is not null-terminated.
This function was introduced in Qt 6.0.
See Also▲
[since 6.1] QStringView::const_iterator QStringView::constEnd() const▲
Same as end().
This function was introduced in Qt 6.1.
See Also▲
See also constBegin(), end(), cend(), crend()
[since 6.1] bool QStringView::contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const▲
Returns true if the regular expression re matches somewhere in this string view; otherwise returns false.
If the match is successful and rmatch is not nullptr, it also writes the results of the match into the QRegularExpressionMatch object pointed to by rmatch.
This function was introduced in Qt 6.1.
See Also▲
See also QRegularExpression::match()
[since 6.1] qsizetype QStringView::count(const QRegularExpression &re) const▲
Returns the number of times the regular expression re matches in the string view.
For historical reasons, this function counts overlapping matches. This behavior is different from simply iterating over the matches in the string view using QRegularExpressionMatchIterator.
This function was introduced in Qt 6.1.
See Also▲
See also QRegularExpression::globalMatch()
[since 6.0] qsizetype QStringView::count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
This function overloads count().
Returns the number of occurrences of the character ch in the string view.
If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.
This function was introduced in Qt 6.0.
See Also▲
See also QString::count(), contains(), indexOf()
[since 6.0] qsizetype QStringView::count(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
This function overloads count().
Returns the number of (potentially overlapping) occurrences of the string view str in this string view.
If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.
This function was introduced in Qt 6.0.
See Also▲
See also QString::count(), contains(), indexOf()
[since 6.4] qsizetype QStringView::count(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
This function overloads count().
Returns the number of (potentially overlapping) occurrences of the Latin-1 string l1 in this string view.
If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.
This function was introduced in Qt 6.4.
See Also▲
See also QString::count(), contains(), indexOf()
QStringView::const_reverse_iterator QStringView::crbegin() const▲
QStringView::const_reverse_iterator QStringView::crend() const▲
QStringView::const_pointer QStringView::data() const▲
Returns a const pointer to the first character in the string view.
The character array represented by the return value is not null-terminated.
See Also▲
[constexpr] bool QStringView::empty() const▲
Returns whether this string view is empty - that is, whether size() == 0.
This function is provided for STL compatibility.
See Also▲
QStringView::const_iterator QStringView::end() const▲
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▲
[constexpr, since 6.0] QStringView QStringView::first(qsizetype n) const▲
Returns a string view that points to the first n characters of this string view.
The behavior is undefined when n < 0 or n > size().
This function was introduced in Qt 6.0.
See Also▲
[constexpr] QChar QStringView::first() const▲
Returns the first character in the string view. Same as front().
This function is provided for compatibility with other Qt containers.
Calling this function on an empty string view constitutes undefined behavior.
See Also▲
[static constexpr] QStringView QStringView::fromArray(const Char (&)[Size] string = Size)▲
Constructs a string view on the full character string literal string, including any trailing Char(0). If you don't want the null-terminator included in the view then you can chop() it off when you are certain it is at the end. Alternatively you can use the constructor overload taking an array literal which will create a view up to, but not including, the first null-terminator in the data.
string must remain valid for the lifetime of this string view object.
This function will work with any array literal 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.
[constexpr] QChar QStringView::front() const▲
Returns the first character in the string view. Same as first().
This function is provided for STL compatibility.
Calling this function on an empty string view constitutes undefined behavior.
See Also▲
[since 6.1] qsizetype QStringView::indexOf(const QRegularExpression &re, qsizetype from = 0, QRegularExpressionMatch *rmatch = nullptr) const▲
Returns the index position of the first match of the regular expression re in the string view, searching forward from index position from. Returns -1 if re didn't match anywhere.
If the match is successful and rmatch is not nullptr, it also writes the results of the match into the QRegularExpressionMatch object pointed to by rmatch.
Due to how the regular expression matching algorithm works, this function will actually match repeatedly from the beginning of the string view until the position from is reached.
This function was introduced in Qt 6.1.
[constexpr] bool QStringView::isEmpty() const▲
Returns whether this string view is empty - that is, whether size() == 0.
This function is provided for compatibility with other Qt containers.
See Also▲
[constexpr] bool QStringView::isNull() const▲
Returns whether this string view is null - that is, whether data() == nullptr.
This functions is provided for compatibility with other Qt containers.
See Also▲
[since 5.11] bool QStringView::isRightToLeft() const▲
Returns true if the string view is read right to left.
This function was introduced in Qt 5.11.
See Also▲
See also QString::isRightToLeft()
[since 5.15] bool QStringView::isValidUtf16() const▲
Returns true if the string view 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▲
See also QString::isValidUtf16()
[constexpr, since 6.0] QStringView QStringView::last(qsizetype n) const▲
Returns a string view that points to the last n characters of this string view.
The behavior is undefined when n < 0 or n > size().
This function was introduced in Qt 6.0.
See Also▲
[constexpr] QChar QStringView::last() const▲
Returns the last character in the string view. Same as back().
This function is provided for compatibility with other Qt containers.
Calling this function on an empty string view constitutes undefined behavior.
See Also▲
[since 6.3] qsizetype QStringView::lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const▲
This function overloads lastIndexOf().
This function was introduced in Qt 6.3.
[since 6.2] qsizetype QStringView::lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const▲
Returns the index position of the last match of the regular expression re in the string view. Returns -1 if re didn't match anywhere.
If the match is successful and rmatch is not nullptr, it also writes the results of the match into the QRegularExpressionMatch object pointed to by rmatch.
Due to how the regular expression matching algorithm works, this function will actually match repeatedly from the beginning of the string view until the end of the string view is reached.
This function was introduced in Qt 6.2.
[since 6.1] qsizetype QStringView::lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch = nullptr) const▲
Returns the index position of the last match of the regular expression re in the string view, which starts before the index position from. If from is -1, the search starts at the last character; if from is -2, at the next to last character and so on. Returns -1 if re didn't match anywhere.
If the match is successful and rmatch is not nullptr, it also writes the results of the match into the QRegularExpressionMatch object pointed to by rmatch.
Due to how the regular expression matching algorithm works, this function will actually match repeatedly from the beginning of the string view until the position from is reached.
When searching for a regular expression re that may match 0 characters, the match at the end of the data is excluded from the search by a negative from, even though -1 is normally thought of as searching from the end of the string view: the match at the end is after the last character, so it is excluded. To include such a final empty match, either give a positive value for from or omit the from parameter entirely.
This function was introduced in Qt 6.1.
[constexpr] qsizetype QStringView::length() const▲
[since 6.4] int QStringView::localeAwareCompare(QStringView other) const▲
Compares this string view with the other string view and returns an integer less than, equal to, or greater than zero if this string view is less than, equal to, or greater than the other string view.
The comparison is performed in a locale- and also platform-dependent manner. Use this function to present sorted lists of strings to the user.
This function was introduced in Qt 6.4.
See Also▲
See also Comparing Strings
QStringView::const_reverse_iterator QStringView::rbegin() const▲
Returns a const STL-style reverse iterator pointing to the first character in the string view, in reverse order.
This function is provided for STL compatibility.
See Also▲
QStringView::const_reverse_iterator QStringView::rend() const▲
Returns a STL-style reverse iterator pointing to one past the last character in the string view, in reverse order.
This function is provided for STL compatibility.
See Also▲
[constexpr] qsizetype QStringView::size() const▲
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).
See Also▲
[constexpr, since 6.0] QStringView QStringView::sliced(qsizetype pos, qsizetype n) const▲
Returns a string view that points to n characters of this string view, starting at position pos.
The behavior is undefined when pos < 0, n < 0, or pos + n > size().
This function was introduced in Qt 6.0.
See Also▲
[constexpr, since 6.0] QStringView QStringView::sliced(qsizetype pos) const▲
This is an overloaded function.
Returns a string view starting at position pos in this object, and extending to its end.
The behavior is undefined when pos < 0 or pos > size().
This function was introduced in Qt 6.0.
See Also▲
[since 6.0] QList<QStringView> QStringView::split(const QRegularExpression &re, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const▲
Splits the string into substring views wherever the regular expression re matches, and returns the list of those strings. If re does not match anywhere in the string, split() returns a single-element list containing this string as view.
The views in the returned list are sub-views of this view; as such, they reference the same data as it and only remain valid for as long as that data remains live.
This function was introduced in Qt 6.0.
[since 6.0] CFStringRef QStringView::toCFString() const▲
Creates a CFString from this QStringView.
The caller owns the CFString and is responsible for releasing it.
this function is only available on macOS and iOS.
This function was introduced in Qt 6.0.
[since 6.0] double QStringView::toDouble(bool *ok = nullptr) const▲
Returns the string view converted to a double value.
Returns an infinity if the conversion overflows or 0.0 if the conversion fails for other reasons (e.g. underflow).
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toDouble()
For historic reasons, this function does not handle thousands group separators. If you need to convert such numbers, use QLocale::toDouble().
This function was introduced in Qt 6.0.
See Also▲
See also QString::toDouble()
[since 6.0] float QStringView::toFloat(bool *ok = nullptr) const▲
Returns the string view converted to a float value.
Returns an infinity if the conversion overflows or 0.0 if the conversion fails for other reasons (e.g. underflow).
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toFloat()
This function was introduced in Qt 6.0.
See Also▲
See also QString::toFloat()
[since 6.0] int QStringView::toInt(bool *ok = nullptr, int base = 10) const▲
Returns the string view converted to an int using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
If base is 0, the C language convention is used: if the string view begins with "0x", base 16 is used; otherwise, if the string view begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toInt()
This function was introduced in Qt 6.0.
See Also▲
See also QString::toInt()
QByteArray QStringView::toLatin1() const▲
Returns a Latin-1 representation of the string as a QByteArray.
The behavior is undefined if the string contains non-Latin1 characters.
See Also▲
See also toUtf8(), toLocal8Bit(), QStringEncoder
QByteArray QStringView::toLocal8Bit() const▲
Returns a local 8-bit representation of the string as a QByteArray.
On Unix systems this is equivalen to toUtf8(), on Windows the systems current code page is being used.
The behavior is undefined if the string contains characters not supported by the locale's 8-bit encoding.
See Also▲
See also toLatin1(), toUtf8(), QStringEncoder
[since 6.0] long QStringView::toLong(bool *ok = nullptr, int base = 10) const▲
Returns the string view converted to a long using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
If base is 0, the C language convention is used: if the string view begins with "0x", base 16 is used; otherwise, if the string view begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toLong()
This function was introduced in Qt 6.0.
See Also▲
See also QString::toLong()
[since 6.0] qlonglong QStringView::toLongLong(bool *ok = nullptr, int base = 10) const▲
Returns the string view converted to a long long using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
If base is 0, the C language convention is used: if the string view begins with "0x", base 16 is used; otherwise, if the string view begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toLongLong()
This function was introduced in Qt 6.0.
See Also▲
See also QString::toLongLong()
[since 6.0] NSString *QStringView::toNSString() const▲
Creates an NSString from this QStringView.
The NSString is autoreleased.
this function is only available on macOS and iOS.
This function was introduced in Qt 6.0.
[since 6.0] short QStringView::toShort(bool *ok = nullptr, int base = 10) const▲
Returns the string view converted to a short using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
If base is 0, the C language convention is used: if the string view begins with "0x", base 16 is used; otherwise, if the string view begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toShort()
This function was introduced in Qt 6.0.
See Also▲
See also QString::toShort()
QString QStringView::toString() const▲
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.
[since 6.0] uint QStringView::toUInt(bool *ok = nullptr, int base = 10) const▲
Returns the string view converted to an unsigned int using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
If base is 0, the C language convention is used: if the string view begins with "0x", base 16 is used; otherwise, if the string view begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toUInt()
This function was introduced in Qt 6.0.
See Also▲
See also QString::toUInt()
[since 6.0] ulong QStringView::toULong(bool *ok = nullptr, int base = 10) const▲
Returns the string view converted to an unsigned long using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
If base is 0, the C language convention is used: if the string view begins with "0x", base 16 is used; otherwise, if the string view begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toULongLong()
This function was introduced in Qt 6.0.
See Also▲
See also QString::toULong()
[since 6.0] qulonglong QStringView::toULongLong(bool *ok = nullptr, int base = 10) const▲
Returns the string view converted to an unsigned long long using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
If base is 0, the C language convention is used: if the string view begins with "0x", base 16 is used; otherwise, if the string view begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toULongLong()
This function was introduced in Qt 6.0.
See Also▲
See also QString::toULongLong()
[since 6.0] ushort QStringView::toUShort(bool *ok = nullptr, int base = 10) const▲
Returns the string view converted to an unsigned short using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
If base is 0, the C language convention is used: if the string view begins with "0x", base 16 is used; otherwise, if the string view begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toUShort()
This function was introduced in Qt 6.0.
See Also▲
See also QString::toUShort()
QList<uint> QStringView::toUcs4() const▲
Returns a UCS-4/UTF-32 representation of the string view as a QList<uint>.
UCS-4 is a Unicode codec and therefore it is lossless. All characters from this string view will be encoded in UCS-4. Any invalid sequence of code units in this string view is replaced by the Unicode replacement character (QChar::ReplacementCharacter, which corresponds to U+FFFD).
The returned list is not 0-terminated.
See Also▲
See also toUtf8(), toLatin1(), toLocal8Bit(), QStringEncoder
QByteArray QStringView::toUtf8() const▲
Returns a UTF-8 representation of the string view as a QByteArray.
UTF-8 is a Unicode codec and can represent all characters in a Unicode string like QString.
See Also▲
See also toLatin1(), toLocal8Bit(), QStringEncoder
[since 5.14] qsizetype QStringView::toWCharArray(wchar_t *array) const▲
Transcribes this string view into the given array.
The caller is responsible for ensuring array is large enough to hold the wchar_t encoding of this string view (allocating the array with the same length as the string view 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.
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▲
See also QString::toWCharArray()
QStringView QStringView::trimmed() const▲
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 ' '.
[constexpr] void QStringView::truncate(qsizetype length)▲
Truncates this string view to length length.
Same as *this = left(length).
The behavior is undefined when length < 0 or length > size().
See Also▲
[constexpr] const QStringView::storage_type *QStringView::utf16() const▲
Returns a const pointer to the first character in the string view.
storage_type is char16_t.
The character array represented by the return value is not null-terminated.
See Also▲
[constexpr] QChar QStringView::operator[](qsizetype n) const▲
Related Non-Members▲
bool operator!=(QStringView lhs, QStringView rhs)▲
bool operator<(QStringView lhs, QStringView rhs)
bool operator<=(QStringView lhs, QStringView rhs)
bool operator==(QStringView lhs, QStringView rhs)
bool operator>(QStringView lhs, QStringView rhs)
bool operator>=(QStringView lhs, QStringView rhs)
Operators for comparing lhs to rhs.
See Also▲
See also compare()
[since 5.10] size_t qHash(QStringView key, size_t seed = 0)▲
Returns the hash value for the key, using seed to seed the calculation.
This function was introduced in Qt 5.10.
Obsolete Members for QStringView▲
The following members of class QStringView are deprecated. We strongly advise against using them in new code.
Obsolete Member Function Documentation▲
[constexpr] QStringView QStringView::left(qsizetype length) const▲
This function is deprecated. We strongly advise against using it in new code.
Use first() instead in new code.
Returns the substring of length length starting at position 0 in this object.
The entire string view is returned if length is greater than or equal to size(), or less than zero.
See Also▲
[constexpr] QStringView QStringView::mid(qsizetype start, qsizetype length = -1) const▲
This function is deprecated. We strongly advise against using it in new code.
Returns the substring of length length starting at position start in this object.
Use sliced() instead in new code.
Returns an empty string view if start exceeds the length of the string view. If there are less than length characters available in the string view starting at start, or if length is negative (default), the function returns all characters that are available from start.
See Also▲
[constexpr] QStringView QStringView::right(qsizetype length) const▲
This function is deprecated. We strongly advise against using it in new code.
Use last() instead in new code.
Returns the substring of length length starting at position size() - length in this object.
The entire string view is returned if length is greater than or equal to size(), or less than zero.