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 value_type

Public Functions

QStringView()
QStringView(std::nullptr_t)
QStringView(const Char *str, qssize_t len)
QStringView(const Char *str)
QStringView(const QString &str)
QStringView(const QStringRef &str)
QStringView(const StdBasicString &str)
QChar at(qssize_t n) const
QChar back() const
const_iterator begin() const
const_iterator cbegin() const
const_iterator cend() const
const_reverse_iterator crbegin() const
const_reverse_iterator crend() const
const_pointer data() const
bool empty() const
const_iterator end() const
QChar first() const
QChar front() const
bool isEmpty() const
bool isNull() const
QChar last() const
QStringView left(qssize_t length) const
int length() const
QStringView mid(qssize_t start) const
QStringView mid(qssize_t start, qssize_t length) const
const_reverse_iterator rbegin() const
const_reverse_iterator rend() const
QStringView right(qssize_t length) const
qssize_t size() const
QByteArray toLatin1() const
QByteArray toLocal8Bit() const
QString toString() const
QVector<uint> toUcs4() const
QByteArray toUtf8() const
const storage_type *utf16() const
QChar operator[](qssize_t n) const
int qCompareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)
int qCompareStrings(QStringView lhs, QLatin1String rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)
int qCompareStrings(QLatin1String lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)
int qCompareStrings(QLatin1String lhs, QLatin1String rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QByteArray qConvertToLatin1(QStringView string)
QByteArray qConvertToLocal8Bit(QStringView string)
QVector<uint> qConvertToUcs4(QStringView string)
QByteArray qConvertToUtf8(QStringView string)
uint qHash(QStringView key, uint seed = 0)

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 (on compilers that support C++11 Unicode strings) 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 qssize_t. Provided for compatibility with the STL.

Unlike other Qt classes, QStringView uses qssize_t 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::value_type

Alias for const QChar. Provided for compatibility with the STL.

Member Function Documentation

QStringView::QStringView()

Constructs a null string view.

See also isNull().

QStringView::QStringView(std::nullptr_t)

Constructs a null string view.

See also isNull().

QStringView::QStringView(const Char *str, qssize_t 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.

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

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

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

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

const_iterator QStringView::cbegin() const

Same as begin().

This function is provided for STL compatibility.

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

const_iterator QStringView::cend() const

Same as end().

This function is provided for STL compatibility.

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

const_reverse_iterator QStringView::crbegin() const

Same as rbegin().

This function is provided for STL compatibility.

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

const_reverse_iterator QStringView::crend() const

Same as rend().

This function is provided for STL compatibility.

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

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

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

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(qssize_t length) const

Returns the substring of length length starting at position 0 in this object.

Note: The behavior is undefined when length < 0 or length > size().

See also mid() and right().

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(qssize_t start) const

Returns the substring starting at position start in this object, and extending to the end of the string.

Note: The behavior is undefined when start < 0 or start > size().

See also left() and right().

QStringView QStringView::mid(qssize_t start, qssize_t length) const

This is an overloaded function.

Returns the substring of length length starting at position start in this object.

Note: The behavior is undefined when start < 0, length < 0, or start + length > size().

See also left() and right().

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

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

Returns the substring of length length starting at position size() - length in this object.

Note: The behavior is undefined when length < 0 or length > size().

See also mid() and left().

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

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(), QTextCodec, and qConvertToLatin1().

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(), QTextCodec, and qConvertToLocal8Bit().

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.

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(), QTextCodec, and qConvertToUtf8().

const storage_type *QStringView::utf16() const

Returns a const pointer to the first character in the string.

storage_type is char16_t, except on MSVC 2013 (which lacks char16_t support), where it is wchar_t instead.

Note: The character array represented by the return value is not null-terminated.

See also begin(), end(), and data().

QChar QStringView::operator[](qssize_t 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().

Related Non-Members

int qCompareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)

Returns an integer that compares to 0 as lhs compares to rhs.

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

Case-sensitive comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-visible strings with QString::localeAwareCompare().

int x = qCompareStrings(u"aUtO", u"AuTo", Qt::CaseInsensitive);  // x == 0
int y = qCompareStrings(u"auto", u"Car", Qt::CaseSensitive);     // y > 0
int z = qCompareStrings(u"auto", u"Car", Qt::CaseInsensitive);   // z < 0

This function was introduced in Qt 5.10.

int qCompareStrings(QStringView lhs, QLatin1String rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)

This is an overloaded function.

Returns an integer that compares to 0 as lhs compares to rhs.

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

Case-sensitive comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-visible strings with QString::localeAwareCompare().

This function was introduced in Qt 5.10.

int qCompareStrings(QLatin1String lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)

This is an overloaded function.

Returns an integer that compares to 0 as lhs compares to rhs.

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

Case-sensitive comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-visible strings with QString::localeAwareCompare().

This function was introduced in Qt 5.10.

int qCompareStrings(QLatin1String lhs, QLatin1String rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive)

This is an overloaded function.

Returns an integer that compares to 0 as lhs compares to rhs.

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

Case-sensitive comparison is based exclusively on the numeric Latin-1 values of the characters and is very fast, but is not what a human would expect. Consider sorting user-visible strings with QString::localeAwareCompare().

This function was introduced in Qt 5.10.

QByteArray qConvertToLatin1(QStringView string)

Returns a Latin-1 representation of string as a QByteArray.

The behavior is undefined if string contains non-Latin1 characters.

This function was introduced in Qt 5.10.

See also QString::toLatin1(), QStringView::toLatin1(), qConvertToUtf8(), qConvertToLocal8Bit(), and qConvertToUcs4().

QByteArray qConvertToLocal8Bit(QStringView string)

Returns a local 8-bit representation of 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 qConvertToLatin1().

The behavior is undefined if string contains characters not supported by the locale's 8-bit encoding.

This function was introduced in Qt 5.10.

See also QString::toLocal8Bit(), QStringView::toLocal8Bit(), qConvertToLatin1(), qConvertToUtf8(), and qConvertToUcs4().

QVector<uint> qConvertToUcs4(QStringView string)

Returns a UCS-4/UTF-32 representation of 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's replacement character (QChar::ReplacementCharacter, which corresponds to U+FFFD).

The returned vector is not NUL terminated.

This function was introduced in Qt 5.10.

See also QString::toUcs4(), QStringView::toUcs4(), qConvertToLatin1(), qConvertToLocal8Bit(), and qConvertToUtf8().

QByteArray qConvertToUtf8(QStringView string)

Returns a UTF-8 representation of string as a QByteArray.

UTF-8 is a Unicode codec and can represent all characters in a Unicode string like QStringView.

This function was introduced in Qt 5.10.

See also QString::toUtf8(), QStringView::toUtf8(), qConvertToLatin1(), qConvertToLocal8Bit(), and qConvertToUcs4().

uint qHash(QStringView key, uint seed = 0)

Returns the hash value for the key, using seed to seed the calculation.

This function was introduced in Qt 5.10.

© 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.