QStringView Class | Qt Core 5.15.19 (original) (raw)

The QStringView class provides a unified view on UTF-16 strings with a read-only subset of the QString API. More...

This class was introduced in Qt 5.10.

Member Function Documentation

QList<QStringView> QStringView::split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

QList<QStringView> QStringView::split(QStringView sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

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.

int QStringView::lastIndexOf(QChar, int, Qt::CaseSensitivity) const

int QStringView::lastIndexOf(QLatin1String, int, Qt::CaseSensitivity) const

int QStringView::lastIndexOf(QStringView, int, Qt::CaseSensitivity) 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 (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().

bool QStringView::contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

bool QStringView::contains(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

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 indexOf().

int QStringView::indexOf(QChar, int, Qt::CaseSensitivity) const

int QStringView::indexOf(QLatin1String, int, Qt::CaseSensitivity) const

int QStringView::indexOf(QStringView, int, Qt::CaseSensitivity) 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 QString::indexOf().

bool QStringView::endsWith(QChar ch) const

bool QStringView::endsWith(QChar ch, Qt::CaseSensitivity cs) const

bool QStringView::endsWith(QLatin1String 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 startsWith().

bool QStringView::startsWith(QChar ch) const

bool QStringView::startsWith(QChar ch, Qt::CaseSensitivity cs) const

bool QStringView::startsWith(QLatin1String 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 endsWith().

int QStringView::compare(QChar ch) const

int QStringView::compare(QChar ch, Qt::CaseSensitivity cs) const

int QStringView::compare(QLatin1String 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.14.

template 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 QLatin1String.

In addition, the following types are also supported: QChar, QLatin1Char.

This function was introduced in Qt 5.14.

See also QString::arg().

template <typename StdBasicString, int> QStringView::QStringView(const StdBasicString &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 isNull() and isEmpty().

QStringView::QStringView(const QStringRef &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().

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().

template 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()

Constructs a null string view.

See also isNull().

QChar QStringView::at(int 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().

QChar QStringView::back() const

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().

QStringView::const_iterator QStringView::begin() const

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().

QStringView::const_iterator QStringView::cbegin() const

Same as begin().

This function is provided for STL compatibility.

See also cend(), begin(), crbegin(), and data().

QStringView::const_iterator QStringView::cend() const

Same as end().

This function is provided for STL compatibility.

See also cbegin(), end(), and crend().

void QStringView::chop(int 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(int 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().

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.

int QStringView::count(QChar, Qt::CaseSensitivity) const

Returns the number of occurrences of the character ch in the string reference.

If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.

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::count(), contains(), and indexOf().

int QStringView::count(QStringView, Qt::CaseSensitivity) const

This is an overloaded function.

Returns the number of (potentially overlapping) occurrences of the string reference str in this string reference.

If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.

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::count(), contains(), and indexOf().

QStringView::const_reverse_iterator QStringView::crbegin() const

Same as rbegin().

This function is provided for STL compatibility.

See also crend(), rbegin(), and cbegin().

QStringView::const_reverse_iterator QStringView::crend() const

Same as rend().

This function is provided for STL compatibility.

See also crbegin(), rend(), and cend().

QStringView::const_pointer QStringView::data() const

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().

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 isEmpty(), isNull(), size(), and length().

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 begin(), cend(), and rend().

QChar QStringView::first() const

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().

QChar QStringView::front() const

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().

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 empty(), isNull(), size(), and length().

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 empty(), isEmpty(), size(), and length().

bool QStringView::isRightToLeft() const

Returns true if the string is read right to left.

This function was introduced in Qt 5.11.

See also QString::isRightToLeft().

bool QStringView::isValidUtf16() const

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().

QChar QStringView::last() const

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().

QStringView QStringView::left(int 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().

int QStringView::length() const

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().

QStringView QStringView::mid(int 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(int start, int 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::const_reverse_iterator QStringView::rbegin() const

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().

QStringView::const_reverse_iterator QStringView::rend() const

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().

QStringView QStringView::right(int 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().

int 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 and QStringRef).

See also empty(), isEmpty(), isNull(), and length().

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().

short QStringView::toShort(bool *ok = nullptr, int base = 10) const

Returns the string 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 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::toShort()

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::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.

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.

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.

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().

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 ' '.

void QStringView::truncate(int length)

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().

const QStringView::storage_type *QStringView::utf16() const

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().

QChar QStringView::operator[](int _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 at(), front(), and back().