6.0.0-rc4Source

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>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QStringView()
QStringView(std::nullptr_t)
QStringView(const QChar *str, qsizetype len)
QStringView(const char16_t *str, qsizetype len)
QStringView(const char16_t *str)
QStringView(const QString &s)
QStringView(std::u16string_view s)
QStringtoString() const
QByteArraytoUtf8() const
QByteArraytoLatin1() const
const QChar *data() const
const QChar *constData() const
const char16_t *utf16() const
qsizetypesize() const
qsizetypelength() const
boolisNull() const
boolisEmpty() const
boolempty() const
QCharat(qsizetype i) const
QCharoperator[](qsizetype i) const
QCharfront() const
QCharback() const
QCharfirst() const
QCharlast() const
const_iteratorbegin() const
const_iteratorend() const
QStringViewmid(qsizetype pos, qsizetype len = -1) const
QStringViewleft(qsizetype c) const
QStringViewright(qsizetype c) const
QStringViewfirst(qsizetype c) const
QStringViewlast(qsizetype c) const
QStringViewsliced(qsizetype pos) const
QStringViewsliced(qsizetype pos, qsizetype c) const
QStringViewchopped(qsizetype c) const
voidtruncate(qsizetype c)
voidchop(qsizetype c)
QStringViewtrimmed() const
boolstartsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
boolendsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetypeindexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetypelastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
boolcontains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
boolstartsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
boolendsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetypeindexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetypelastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
boolcontains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
intcompare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
boolcontains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
qsizetypeindexOf(const QRegularExpression &re, qsizetype from = 0, QRegularExpressionMatch *rmatch = nullptr) const
qsizetypelastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
qsizetypelastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch = nullptr) const
qsizetypecount(const QRegularExpression &re) const
inttoInt(bool *ok = nullptr, int base = 10) const
uinttoUInt(bool *ok = nullptr, int base = 10) const
qlonglongtoLongLong(bool *ok = nullptr, int base = 10) const
qulonglongtoULongLong(bool *ok = nullptr, int base = 10) const
doubletoDouble(bool *ok = nullptr) const
const_iteratorcbegin() const
const_iteratorcend() const
const_reverse_iteratorrbegin() const
const_reverse_iteratorrend() const
const_reverse_iteratorcrbegin() const
const_reverse_iteratorcrend() const
boolstartsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
boolendsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetypeindexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetypelastIndexOf(QLatin1StringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
boolcontains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
intcompare(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
intcompare(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetypecount(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetypecount(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetypecount(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) 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
QByteArraytoLocal8Bit() const
QList<uint>toUcs4() const
boolisValidUtf16() const
boolisUpper() const
boolisLower() const
boolisRightToLeft() const
shorttoShort(bool *ok = nullptr, int base = 10) const
ushorttoUShort(bool *ok = nullptr, int base = 10) const
longtoLong(bool *ok = nullptr, int base = 10) const
ulongtoULong(bool *ok = nullptr, int base = 10) const
floattoFloat(bool *ok = nullptr) const
const_iteratorconstBegin() const
const_iteratorconstEnd() const
QStringView &slice(qsizetype pos)
QStringView &slice(qsizetype pos, qsizetype c)
qsizetypetoWCharArray(wchar_t *array) const
intlocaleAwareCompare(QStringView other) const
QStringarg(const A &a, const Rest &...rest) const
autotokenize(Needle &&needle, Flags... flags) const

Static Public Members

QStringViewfromArray(const char16_t (&string)[N])
qsizetypemax_size()
qsizetypemaxSize()

Detailed Description

A QStringView refers to a contiguous range of QChars without owning or copying it, so the viewed data must outlive the view. It is cheap to copy and is meant for function parameters that only read text, accepting a QString, a u"" literal or a std::u16string_view alike.

A view is null if it was constructed without data (or from a null QString), and empty if its size is 0. A null view is also empty.

See also QString, QStringList and QLatin1StringView.

Differences from Qt

The searching, comparison and conversion functions copy the view into a QString first, so they allocate. arg() takes strings and characters only. first(), last(), sliced(), chopped(), slice(), truncate() and chop() do not check their arguments.

Member Type Documentation

typedef QStringView::const_iterator

A read-only iterator over the characters of the view. It is a plain const QChar *.

typedef QStringView::const_reverse_iterator

A reverse iterator over the characters.

typedef QStringView::iterator

The iterator type, for STL compatibility. It is the same as const_iterator.

typedef QStringView::reverse_iterator

The same as const_reverse_iterator.

typedef QStringView::value_type

The element type of the view, for STL compatibility.

Member Function Documentation

[constexpr] QStringView::QStringView()

Constructs a null view.

See also isNull().

[constexpr] QStringView::QStringView(std::nullptr_t)

This is an overloaded function.

Constructs a null view.

[constexpr] QStringView::QStringView(const QChar *str, qsizetype len)

This is an overloaded function.

Constructs a view of the len characters starting at str.

QStringView::QStringView(const char16_t *str, qsizetype len)

This is an overloaded function.

Constructs a view of the len UTF-16 code units starting at str.

QStringView::QStringView(const char16_t *str)

This is an overloaded function.

Constructs a view of the null-terminated UTF-16 string str. The view is null if str is nullptr.

QStringView::QStringView(const QString &s)

This is an overloaded function.

Constructs a view of the characters of the string s. The view is null if s is null. It is invalidated when s is modified or destroyed.

QStringView::QStringView(std::u16string_view s)

This is an overloaded function.

Constructs a view of the UTF-16 string view s.

template <typename A, typename... Rest>QString QStringView::arg(const A &a, const Rest &...rest) const

Returns a string in which the place markers of this view (%1 to %99) are replaced by a and rest: the lowest-numbered marker by a, the next one by the first of rest, and so on. The arguments are strings, views or characters. The markers are replaced in one pass, so a marker inside an argument is left alone.

QStringView pattern = u"%1 of %2";
const QString text = pattern.arg(u"3", QLatin1StringView("7")); // "3 of 7"

See also QString::arg().

QChar QStringView::at(qsizetype i) const

Returns the character at index position i. i must be a valid index; no bounds checking is done.

See also front() and back().

QChar QStringView::back() const

Returns the last character of the view. The view must not be empty.

See also front() and last().

const_iterator QStringView::begin() const

Returns an iterator pointing to the first character of the view.

See also end().

const_iterator QStringView::cbegin() const

Returns a const iterator pointing to the first character of the view. This is the same as begin().

See also cend().

const_iterator QStringView::cend() const

Returns a const iterator pointing just after the last character of the view. This is the same as end().

See also cbegin().

void QStringView::chop(qsizetype c)

Removes c characters from the end of the view. c must not exceed size(); no bounds checking is done.

See also truncate() and chopped().

QStringView QStringView::chopped(qsizetype c) const

Returns the view with the last c characters removed. c must not exceed size(); no bounds checking is done.

See also chop() and sliced().

int QStringView::compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Compares the view with s, using the case sensitivity cs, and returns a negative integer, zero or a positive integer if the view is less than, equal to or greater than s. See QString::compare().

int QStringView::compare(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Compares the view with s, using cs.

int QStringView::compare(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Compares the view with c, using cs.

const_iterator QStringView::constBegin() const

Returns an iterator pointing to the first character of the view. This is the same as begin().

This function was introduced in 6.1.

[constexpr] const QChar * QStringView::constData() const

Returns a pointer to the first character of the view. This is the same as data().

const_iterator QStringView::constEnd() const

Returns an iterator pointing just after the last character of the view. This is the same as end().

This function was introduced in 6.1.

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

Returns true if the view contains s, using the case sensitivity cs; otherwise returns false.

See also indexOf().

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

This is an overloaded function.

Returns true if the view contains the character c, using the case sensitivity cs; otherwise returns false.

bool QStringView::contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const

Returns true if re matches somewhere in the viewed text; otherwise returns false. If rmatch is not nullptr, a match is stored there.

This function was introduced in 6.1.

bool QStringView::contains(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns true if the view contains s, compared with cs.

qsizetype QStringView::count(const QRegularExpression &re) const

Returns the number of (possibly overlapping) matches of re in the viewed text.

This function was introduced in 6.1.

qsizetype QStringView::count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns how often s occurs in the view, possibly overlapping, compared with cs.

qsizetype QStringView::count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns how often c occurs, compared with cs.

qsizetype QStringView::count(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns how often s occurs, compared with cs.

const_reverse_iterator QStringView::crbegin() const

Returns a reverse iterator at the last character. Same as rbegin().

const_reverse_iterator QStringView::crend() const

Returns a reverse iterator before the first character. Same as rend().

[constexpr] const QChar * QStringView::data() const

Returns a pointer to the first character of the view. The data is not necessarily null-terminated.

See also constData() and utf16().

[constexpr] bool QStringView::empty() const

Returns true if the view has no characters. This is the same as isEmpty(), provided for STL compatibility.

const_iterator QStringView::end() const

Returns an iterator pointing just after the last character of the view.

See also begin().

bool QStringView::endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns true if the view ends with s, using the case sensitivity cs; otherwise returns false.

See also startsWith().

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

This is an overloaded function.

Returns true if the view ends with the character c, using the case sensitivity cs; otherwise returns false.

bool QStringView::endsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns true if the view ends with s, compared with cs.

QChar QStringView::first() const

This is an overloaded function.

Returns the first character of the view. This is the same as front().

QStringView QStringView::first(qsizetype c) const

This is an overloaded function.

Returns a view of the first c characters. c must not exceed size(); no bounds checking is done.

template <std::size_t N>[static] QStringView QStringView::fromArray(const char16_t (&string)[N])

Returns a view of the whole array string, including a terminating null if there is one: fromArray(u"ab") has three characters, where QStringView(u"ab") has two.

QChar QStringView::front() const

Returns the first character of the view. The view must not be empty.

See also back() and first().

qsizetype QStringView::indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns the index position of the first occurrence of s in the view, searching forward from index position from, or -1 if it is not found. cs selects the case sensitivity.

See also lastIndexOf() and contains().

qsizetype QStringView::indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns the index position of the first occurrence of the character c in the view, searching forward from index position from, or -1 if it is not found. cs selects the case sensitivity.

qsizetype QStringView::indexOf(const QRegularExpression &re, qsizetype from = 0, QRegularExpressionMatch *rmatch = nullptr) const

Returns the index position of the first match of re in the viewed text, searching from from, or -1. If rmatch is not nullptr, the match is stored there.

This function was introduced in 6.1.

qsizetype QStringView::indexOf(QLatin1StringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns the position of the first s at or after from, or -1. cs selects case sensitivity.

[constexpr] bool QStringView::isEmpty() const

Returns true if the view has no characters; otherwise returns false.

See also isNull() and size().

bool QStringView::isLower() const

Returns true if the view equals its lowercase form.

This function was introduced in 6.7.

[constexpr] bool QStringView::isNull() const

Returns true if the view has no data (it was default-constructed or built from a null string or pointer); otherwise returns false.

See also isEmpty().

bool QStringView::isRightToLeft() const

Returns true if the view reads right to left, as QString::isRightToLeft() decides.

This function was introduced in 6.0.

bool QStringView::isUpper() const

Returns true if the view equals its uppercase form.

This function was introduced in 6.7.

bool QStringView::isValidUtf16() const

Returns true if the view is valid UTF-16.

QChar QStringView::last() const

This is an overloaded function.

Returns the last character of the view. This is the same as back().

QStringView QStringView::last(qsizetype c) const

This is an overloaded function.

Returns a view of the last c characters. c must not exceed size(); no bounds checking is done.

qsizetype QStringView::lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns the index position of the last occurrence of s in the view, searching backward from index position from, or -1 if it is not found. cs selects the case sensitivity.

See also indexOf().

qsizetype QStringView::lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns the index position of the last occurrence of the character c in the view, searching backward from index position from, or -1 if it is not found. cs selects the case sensitivity.

qsizetype QStringView::lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const

Returns the index position of the last match of re in the viewed text, or -1. If rmatch is not nullptr, the match is stored there.

This function was introduced in 6.1.

qsizetype QStringView::lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch = nullptr) const

This is an overloaded function.

Returns the index position of the last match of re that starts at or before from, or -1. If rmatch is not nullptr, the match is stored there.

This function was introduced in 6.1.

qsizetype QStringView::lastIndexOf(QLatin1StringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns the position of the last s at or before from, or -1. cs selects case sensitivity.

QStringView QStringView::left(qsizetype c) const

Returns the first c characters of the view, or the whole view if c is larger than size().

See also right(), mid() and first().

[constexpr] qsizetype QStringView::length() const

Returns the number of characters in the view. This is the same as size().

int QStringView::localeAwareCompare(QStringView other) const

Compares the view with other as text in the current locale and returns a negative number, zero or a positive number if the view sorts before, with or after other.

This function was introduced in 6.4.

See also QString::localeAwareCompare() and QCollator.

[static, constexpr] qsizetype QStringView::max_size()

Returns the largest number of characters a view can refer to.

This function was introduced in 6.8.

[static, constexpr] qsizetype QStringView::maxSize()

Returns the largest number of characters a view can refer to. This is the same as max_size().

This function was introduced in 6.8.

QStringView QStringView::mid(qsizetype pos, qsizetype len = -1) const

Returns a view of len characters starting at index position pos. If len is negative or extends past the end, the view extends to the end. A negative pos is clamped to 0 and reduces len accordingly. If pos is past the end, an empty view is returned.

See also left(), right() and sliced().

QChar QStringView::operator[](qsizetype i) const

Returns the character at index position i. This is the same as at().

const_reverse_iterator QStringView::rbegin() const

Returns a reverse iterator at the last character.

const_reverse_iterator QStringView::rend() const

Returns a reverse iterator before the first character.

Returns the last c characters of the view, or the whole view if c is larger than size().

See also left(), mid() and last().

[constexpr] qsizetype QStringView::size() const

Returns the number of characters in the view.

See also isEmpty().

QStringView & QStringView::slice(qsizetype pos)

Changes the view to start at index position pos and to extend to its end, and returns a reference to it. pos must not exceed size().

This function was introduced in 6.8.

See also sliced().

QStringView & QStringView::slice(qsizetype pos, qsizetype c)

This is an overloaded function.

Changes the view to the c characters that start at index position pos, and returns a reference to it. The range must lie within the view.

This function was introduced in 6.8.

QStringView QStringView::sliced(qsizetype pos) const

Returns a view of the part starting at index position pos and extending to the end. pos must not exceed size(); no bounds checking is done.

See also mid() and chopped().

QStringView QStringView::sliced(qsizetype pos, qsizetype c) const

This is an overloaded function.

Returns a view of c characters starting at index position pos. The range must lie within the view; no bounds checking is done.

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

Splits the view where sep occurs, compared with cs, and returns views of the parts, which refer to the same characters. With Qt::SkipEmptyParts in behavior, empty parts are left out.

See also QString::split().

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

This is an overloaded function.

Splits the view where the character sep occurs. behavior and cs are as for the other overload.

bool QStringView::startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns true if the view starts with s, using the case sensitivity cs; otherwise returns false.

See also endsWith().

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

This is an overloaded function.

Returns true if the view starts with the character c, using the case sensitivity cs; otherwise returns false.

bool QStringView::startsWith(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns true if the view starts with s, compared with cs.

double QStringView::toDouble(bool *ok = nullptr) const

Returns the view converted to a double. See QString::toDouble() for how ok is set and which inputs are accepted.

float QStringView::toFloat(bool *ok = nullptr) const

Returns the view converted to a float, or 0; *ok reports success if ok is not nullptr.

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

Returns the view converted to an int using base. See QString::toInt() for how ok is set and which inputs are accepted.

See also toUInt() and toLongLong().

template <typename Needle, typename... Flags>auto QStringView::tokenize(Needle &&needle, Flags... flags) const

Returns a tokenizer that splits this view at each occurrence of needle, without copying the text. flags may hold a Qt::SplitBehavior and a Qt::CaseSensitivity, in either order. Include <QStringTokenizer> to use this function.

for (QStringView field : QStringView(u"a,b,,c").tokenize(u',', Qt::SkipEmptyParts))
    use(field); // "a", "b", "c"

This function was introduced in 6.0.

See also QStringTokenizer and split().

QByteArray QStringView::toLatin1() const

Returns the viewed characters as a Latin-1 encoded byte array. Characters outside Latin-1 are replaced by '?'.

See also QString::toLatin1().

QByteArray QStringView::toLocal8Bit() const

Returns the view as UTF-8, the 8-bit encoding of Nt.

long QStringView::toLong(bool *ok = nullptr, int base = 10) const

Returns the view converted to a long in base, or 0; *ok reports success if ok is not nullptr.

qlonglong QStringView::toLongLong(bool *ok = nullptr, int base = 10) const

Returns the view converted to a long long using base. See QString::toLongLong() for how ok is set.

See also toULongLong() and toInt().

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

Returns the view converted to a short in base, or 0 on failure; *ok reports success if ok is not nullptr.

QString QStringView::toString() const

Returns a copy of the viewed characters as a QString. The result is null if the view is null.

See also toUtf8() and toLatin1().

QList<uint> QStringView::toUcs4() const

Returns the code points of the view; unpaired surrogates become U+FFFD.

uint QStringView::toUInt(bool *ok = nullptr, int base = 10) const

Returns the view converted to an unsigned int using base. See QString::toUInt() for how ok is set.

See also toInt().

ulong QStringView::toULong(bool *ok = nullptr, int base = 10) const

Returns the view converted to an unsigned long in base, or 0; *ok reports success if ok is not nullptr.

qulonglong QStringView::toULongLong(bool *ok = nullptr, int base = 10) const

Returns the view converted to an unsigned long long using base. See QString::toULongLong() for how ok is set.

See also toLongLong().

ushort QStringView::toUShort(bool *ok = nullptr, int base = 10) const

Returns the view converted to an unsigned short in base, or 0; *ok reports success if ok is not nullptr.

QByteArray QStringView::toUtf8() const

Returns the viewed characters as a UTF-8 encoded byte array.

See also QString::toUtf8().

qsizetype QStringView::toWCharArray(wchar_t *array) const

Copies the view to array as a wide string and returns the number of wchar_t written. Where wchar_t has 32 bits, a surrogate pair becomes one element. array must have room for size() elements; no terminating null is written.

See also QString::toWCharArray().

QStringView QStringView::trimmed() const

Returns a view with white space removed from the start and the end.

See also QChar::isSpace() and QString::trimmed().

void QStringView::truncate(qsizetype c)

Truncates the view to c characters. c must not exceed size(); no bounds checking is done.

See also chop().

const char16_t * QStringView::utf16() const

Returns a pointer to the first character of the view as UTF-16 code units. The data is not necessarily null-terminated.

See also data().