QStringView Class
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 |
Note: All functions in this class are reentrant.
Public Types
| 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 |
Public Functions
| QChar | at(qsizetype n) const |
| void | chop(qsizetype length) |
| QStringView | chopped(qsizetype length) const |
| QStringView | left(qsizetype length) const |
| QStringView | mid(qsizetype start) const |
| QStringView | mid(qsizetype start, qsizetype length) const |
| QStringView | right(qsizetype length) const |
| QList<QStringView> | split(QStringView sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| QList<QStringView> | split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
| QList<QStringView> | split(const QRegularExpression &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const |
| double | toDouble(bool *ok = nullptr) const |
| float | toFloat(bool *ok = nullptr) const |
| int | toInt(bool *ok = nullptr, int base = 10) const |
| QByteArray | toLatin1() const |
| QByteArray | toLocal8Bit() const |
| long | toLong(bool *ok = nullptr, int base = 10) const |
| qlonglong | toLongLong(bool *ok = nullptr, int base = 10) const |
| uint | toUInt(bool *ok = nullptr, int base = 10) const |
| ulong | toULong(bool *ok = nullptr, int base = 10) const |
| qulonglong | toULongLong(bool *ok = nullptr, int base = 10) const |
| ushort | toUShort(bool *ok = nullptr, int base = 10) const |
| QVector<uint> | toUcs4() const |
| QByteArray | toUtf8() const |
| int | toWCharArray(wchar_t *array) const |
| void | truncate(qsizetype length) |
Detailed Description
The QStringView class provides a unified view on UTF-16 strings with a read-only subset of the QString API.
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
- QChar: this overload can delegate to the QStringView version:
void fun(QChar ch) { fun(QStringView(&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.
- QLatin1String: if you can implement the function without converting the QLatin1String to UTF-16 first; users expect a function overloaded on QLatin1String to perform strictly less memory allocations than the semantically equivalent call of the QStringView version, involving construction of a QString from the QLatin1String.
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.
Member Type Documentation
typedef QStringView::const_iterator
This typedef provides an STL-style const iterator for QStringView.
See also iterator and const_reverse_iterator.
typedef QStringView::const_pointer
Alias for value_type *. Provided for compatibility with the STL.
typedef QStringView::const_reference
Alias for value_type &. Provided for compatibility with the STL.
typedef QStringView::const_reverse_iterator
This typedef provides an STL-style const reverse iterator for QStringView.
See also reverse_iterator and const_iterator.
typedef QStringView::difference_type
Alias for std::ptrdiff_t. Provided for compatibility with the STL.
typedef 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 const_iterator and reverse_iterator.
typedef 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.
typedef 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.
typedef 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 const_reverse_iterator and iterator.
typedef QStringView::size_type
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.
typedef QStringView::storage_type
Alias for char16_t.
typedef QStringView::value_type
Alias for const QChar. Provided for compatibility with the STL.
Member Function Documentation
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 operator[](), front(), and back().
void QStringView::chop(qsizetype length)
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().
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).
Note: The behavior is undefined when length < 0 or length > size().
See also mid(), left(), right(), chop(), and truncate().
QStringView QStringView::left(qsizetype length) const
Returns the substring of length length starting at position 0 in this object.
Note: Until 5.15.1, the behavior was undefined when length < 0 or length > size(). Since 5.15.2, the behavior is compatible with QString::left().
See also mid(), right(), chopped(), chop(), and truncate().
QStringView QStringView::mid(qsizetype start) const
Returns the substring starting at position start in this object, and extending to the end of the string.
Note: Until 5.15.1, the behavior was undefined when start < 0 or start > size(). Since 5.15.2, the behavior is compatible with QString::mid().
See also left(), right(), chopped(), chop(), and truncate().
QStringView QStringView::mid(qsizetype start, qsizetype length) const
This is an overloaded function.
Returns the substring of length length starting at position start in this object.
Note: Until 5.15.1, the behavior was undefined when start < 0, length < 0, or start + length > size(). Since 5.15.2, the behavior is compatible with QString::mid().
See also left(), right(), chopped(), chop(), and truncate().
QStringView QStringView::right(qsizetype length) const
Returns the substring of length length starting at position size() - length in this object.
Note: Until 5.15.1, the behavior was undefined when length < 0 or length > size(). Since 5.15.2, the behavior is compatible with QString::right().
See also mid(), left(), chopped(), chop(), and truncate().
QList<QStringView> QStringView::split(QStringView sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Splits the string into substrings wherever sep occurs, and returns the list of those strings.
See QString::split() for how sep, behavior and cs interact to form the result.
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Splits the string into substrings wherever sep occurs, and returns the list of those strings.
See QString::split() for how sep, behavior and cs interact to form the result.
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
QList<QStringView> QStringView::split(const QRegularExpression &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const
Splits the string into substrings wherever sep occurs, and returns the list of those strings.
See QString::split() for how sep, behavior and cs interact to form the result.
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
double QStringView::toDouble(bool *ok = nullptr) const
Returns the string 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().
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
See also QString::toDouble().
float QStringView::toFloat(bool *ok = nullptr) const
Returns the string 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()
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
See also QString::toFloat().
int QStringView::toInt(bool *ok = nullptr, int base = 10) const
Returns the string 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 begins with "0x", base 16 is used; if the string 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()
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
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 toUtf8(), toLocal8Bit(), and QTextCodec.
QByteArray QStringView::toLocal8Bit() const
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.
long QStringView::toLong(bool *ok = nullptr, int base = 10) const
Returns the string 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 begins with "0x", base 16 is used; if the string 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()
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
See also QString::toLong().
qlonglong QStringView::toLongLong(bool *ok = nullptr, int base = 10) const
Returns the string 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 begins with "0x", base 16 is used; if the string 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()
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
See also QString::toLongLong().
uint QStringView::toUInt(bool *ok = nullptr, int base = 10) const
Returns the string 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 begins with "0x", base 16 is used; if the string 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()
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
See also QString::toUInt().
ulong QStringView::toULong(bool *ok = nullptr, int base = 10) const
Returns the string 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 begins with "0x", base 16 is used; if the string 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()
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
See also QString::toULong().
qulonglong QStringView::toULongLong(bool *ok = nullptr, int base = 10) const
Returns the string 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 begins with "0x", base 16 is used; if the string 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()
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
See also QString::toULongLong().
ushort QStringView::toUShort(bool *ok = nullptr, int base = 10) const
Returns the string 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 begins with "0x", base 16 is used; if the string 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()
Note: This method has been added in 5.15.2 to simplify writing code that is portable between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5.
This function was introduced in Qt 5.15.2.
See also QString::toUShort().
QVector<uint> QStringView::toUcs4() const
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.
QByteArray QStringView::toUtf8() const
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.
int QStringView::toWCharArray(wchar_t *array) const
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().
void QStringView::truncate(qsizetype length)
Truncates this string view to length length.
Same as *this = left(length).
Note: The behavior is undefined when length < 0 or length > size().