QStringView 类

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

头: #include <QStringView>
qmake: QT += core
Since: Qt 5.10

注意: 此类的所有函数 可重入 .

公共类型

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

公共函数

QStringView ()
QStringView ( std::nullptr_t )
QStringView (const Char * str , qsizetype len )
QStringView (const Char * first , const Char * last )
QStringView (const Char (&)[N] string = ...)
QStringView (const Char * str )
QStringView (const QString & str )
QStringView (const QStringRef & str )
QStringView (const StdBasicString & str )
QChar at (qsizetype n ) const
QChar back () const
QStringView::const_iterator begin () const
QStringView::const_iterator cbegin () const
QStringView::const_iterator cend () const
void chop (qsizetype length )
QStringView chopped (qsizetype length ) const
int compare (QStringView other , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QStringView::const_reverse_iterator crbegin () const
QStringView::const_reverse_iterator crend () const
QStringView::const_pointer data () const
bool empty () const
QStringView::const_iterator end () const
bool endsWith (QStringView str , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool endsWith (QLatin1String l1 , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool endsWith (QChar ch ) const
bool endsWith (QChar ch , Qt::CaseSensitivity cs ) const
QChar first () const
QChar front () const
bool isEmpty () const
bool isNull () const
bool isRightToLeft () const
QChar last () const
QStringView left (qsizetype length ) const
int length () const
QStringView mid (qsizetype start ) const
QStringView mid (qsizetype start , qsizetype length ) const
QStringView::const_reverse_iterator rbegin () const
QStringView::const_reverse_iterator rend () const
QStringView right (qsizetype length ) const
qsizetype size () const
bool startsWith (QStringView str , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool startsWith (QLatin1String l1 , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool startsWith (QChar ch ) const
bool startsWith (QChar ch , Qt::CaseSensitivity cs ) const
QByteArray toLatin1 () const
QByteArray toLocal8Bit () const
QString toString () const
QVector<uint> toUcs4 () const
QByteArray toUtf8 () const
QStringView trimmed () const
void truncate (qsizetype length )
const QStringView::storage_type * utf16 () const
QChar operator[] (qsizetype n ) const
uint qHash (QStringView key , uint seed = ...)

详细描述

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 首先。

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 ,和 (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" char16_t 字符串文字。

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

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

QStringView 文字类型 , 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 () 在 constexpr contexts instead.

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

另请参阅 QString and QStringRef .

成员类型文档编制

typedef QStringView:: const_iterator

此 typedef 提供 STL 样式 const 迭代器为 QStringView .

另请参阅 iterator and const_reverse_iterator .

typedef QStringView:: const_pointer

别名化的 value_type * 。为兼容 STL (标准模板库) 提供。

typedef QStringView:: const_reference

别名化的 value_type & 。为兼容 STL (标准模板库) 提供。

typedef QStringView:: const_reverse_iterator

This typedef provides an STL-style const reverse iterator for QStringView .

另请参阅 reverse_iterator and const_iterator .

typedef QStringView:: difference_type

别名化的 std::ptrdiff_t 。为兼容 STL (标准模板库) 提供。

typedef QStringView:: iterator

此 typedef 提供 STL 样式 const 迭代器为 QStringView .

QStringView does not support mutable iterators, so this is the same as const_iterator .

另请参阅 const_iterator and reverse_iterator .

typedef QStringView:: pointer

别名化的 value_type * 。为兼容 STL (标准模板库) 提供。

QStringView does not support mutable pointers, so this is the same as const_pointer .

typedef QStringView:: reference

别名化的 value_type & 。为兼容 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 .

另请参阅 const_reverse_iterator and iterator .

typedef QStringView:: size_type

别名化的 qsizetype。为兼容 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

别名化的 char16_t for non-Windows or if Q_COMPILER_UNICODE_STRINGS is defined. Otherwise, alias for wchar_t .

typedef QStringView:: value_type

别名化的 const QChar 。为兼容 STL (标准模板库) 提供。

成员函数文档编制

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

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

bool QStringView:: endsWith ( QChar ch ) const

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

返回 true if this string-view ends with string-view str , Latin-1 string l1 , or character ch , respectively; otherwise returns false .

cs is Qt::CaseSensitive (默认),搜索区分大小写;否则,搜索不区分大小写。

另请参阅 startsWith ().

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

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

bool QStringView:: startsWith ( QChar ch ) const

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

返回 true if this string-view starts with string-view str , Latin-1 string l1 , or character ch , respectively; otherwise returns false .

cs is Qt::CaseSensitive (默认),搜索区分大小写;否则,搜索不区分大小写。

另请参阅 endsWith ().

QStringView:: QStringView ()

构造 null 字符串视图。

另请参阅 isNull ().

QStringView:: QStringView ( std::nullptr_t )

构造 null 字符串视图。

另请参阅 isNull ().

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

构造字符串视图对 str 按长度 len .

范围 [str,len) must remain valid for the lifetime of this string view object.

传递 nullptr as str 是安全的若 len is 0, too, and results in a null string view.

行为未定义若 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 * first , const Char * last )

构造字符串视图对 first 按长度 ( last - first ).

范围 [first,last) must remain valid for the lifetime of this string view object.

传递 nullptr as first 是安全的若 last is nullptr, too, and results in a null string view.

行为未定义若 last precedes first ,或 first is nullptr and last is not.

This constructor only participates in overload resolution if Char is a compatible character type. The compatible character types are: QChar , ushort , char16_t and (on platforms, such as Windows, where it is a 16-bit type) wchar_t .

QStringView:: QStringView (const Char (&)[ N ] string = ...)

Constructs a string view on the character string literal string . The length is set to N-1 , excluding the trailing {Char(0)}. If you need the full array, use the constructor from pointer and size instead:

    auto sv = QStringView(array, std::size(array)); // using C++17 std::size()
					

string must remain valid for the lifetime of this string view object.

This constructor only participates in overload resolution if string is an actual array and Char is a compatible character type. The compatible character types are: QChar , ushort , char16_t and (on platforms, such as Windows, where it is a 16-bit type) wchar_t .

QStringView:: QStringView (const Char * str )

构造字符串视图对 str . The length is determined by scanning for the first Char(0) .

str must remain valid for the lifetime of this string view object.

传递 nullptr as str 是安全的且结果在 null 字符串视图中。

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 )

构造字符串视图对 str .

str.data() must remain valid for the lifetime of this string view object.

字符串视图将为 null 当且仅当 str.isNull() .

QStringView:: QStringView (const QStringRef & str )

构造字符串视图对 str .

str.data() must remain valid for the lifetime of this string view object.

字符串视图将为 null 当且仅当 str.isNull() .

QStringView:: QStringView (const StdBasicString & str )

构造字符串视图对 str 。长度取自 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).

另请参阅 isNull () 和 isEmpty ().

QChar QStringView:: at ( qsizetype n ) const

返回字符位于位置 n 在此字符串视图。

行为未定义若 n is negative or not less than size ().

另请参阅 operator[] (), front (),和 back ().

QChar QStringView:: back () const

返回字符串中的最后一个字符。如同 last ().

此函数为兼容 STL (标准模板库) 提供。

警告: 在空字符串视图调用此函数,将构成未定义行为。

另请参阅 front (), first (),和 last ().

QStringView::const_iterator QStringView:: begin () const

返回常量 STL 样式迭代器 指向字符串中的首个字符。

此函数为兼容 STL (标准模板库) 提供。

另请参阅 end (), cbegin (), rbegin (),和 data ().

QStringView::const_iterator QStringView:: cbegin () const

如同 begin ().

此函数为兼容 STL (标准模板库) 提供。

另请参阅 cend (), begin (), crbegin (),和 data ().

QStringView::const_iterator QStringView:: cend () const

如同 end ().

此函数为兼容 STL (标准模板库) 提供。

另请参阅 cbegin (), end (),和 crend ().

void QStringView:: chop ( qsizetype length )

截取此字符串视图按 length 字符。

如同 *this = left(size() - length) .

注意: 行为未定义当 length < 0 or length > size ().

另请参阅 mid (), left (), right (), chopped (),和 truncate ().

QStringView QStringView:: chopped ( qsizetype length ) const

返回子字符串长度 size () - length 起始于此对象的开头。

如同 left(size() - length) .

注意: 行为未定义当 length < 0 or length > size ().

另请参阅 mid (), left (), right (), chop (),和 truncate ().

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

Compares this string-view with the other string-view and returns an integer less than, equal to, or greater than zero if this string-view is less than, equal to, or greater than the other string-view.

cs is Qt::CaseSensitive , the comparison is case sensitive; otherwise the comparison is case insensitive.

该函数在 Qt 5.12 引入。

另请参阅 operator== (), operator< (),和 operator> ().

QStringView::const_reverse_iterator QStringView:: crbegin () const

如同 rbegin ().

此函数为兼容 STL (标准模板库) 提供。

另请参阅 crend (), rbegin (),和 cbegin ().

QStringView::const_reverse_iterator QStringView:: crend () const

如同 rend ().

此函数为兼容 STL (标准模板库) 提供。

另请参阅 crbegin (), rend (),和 cend ().

QStringView::const_pointer QStringView:: data () const

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

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

另请参阅 begin (), end (),和 utf16 ().

bool QStringView:: empty () const

Returns whether this string view is empty - that is, whether size() == 0 .

此函数为兼容 STL (标准模板库) 提供。

另请参阅 isEmpty (), isNull (), size (),和 length ().

QStringView::const_iterator QStringView:: end () const

返回常量 STL 样式迭代器 pointing to the imaginary character after the last character in the list.

此函数为兼容 STL (标准模板库) 提供。

另请参阅 begin (), cend (),和 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.

警告: 在空字符串视图调用此函数,将构成未定义行为。

另请参阅 front (), back (),和 last ().

QChar QStringView:: front () const

Returns the first character in the string. Same as first ().

此函数为兼容 STL (标准模板库) 提供。

警告: 在空字符串视图调用此函数,将构成未定义行为。

另请参阅 back (), first (),和 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.

另请参阅 empty (), isNull (), size (),和 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.

另请参阅 empty (), isEmpty (), size (),和 length ().

bool QStringView:: isRightToLeft () const

返回 true if the string is read right to left.

该函数在 Qt 5.11 引入。

另请参阅 QString::isRightToLeft ().

QChar QStringView:: last () const

返回字符串中的最后一个字符。如同 back ().

This function is provided for compatibility with other Qt containers.

警告: 在空字符串视图调用此函数,将构成未定义行为。

另请参阅 back (), front (),和 first ().

QStringView QStringView:: left ( qsizetype length ) const

返回子字符串长度 length starting at position 0 in this object.

注意: 行为未定义当 length < 0 or length > size ().

另请参阅 mid (), right (), chopped (), chop (),和 truncate ().

int QStringView:: length () const

如同 size (), except returns the result as an int .

This function is provided for compatibility with other Qt containers.

警告: QStringView can represent strings with more than 2 31 characters. Calling this function on a string view for which size () returns a value greater than INT_MAX constitutes undefined behavior.

另请参阅 empty (), isEmpty (), isNull (),和 size ().

QStringView QStringView:: mid ( qsizetype start ) const

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

注意: 行为未定义当 start < 0 or start > size ().

另请参阅 left (), right (), chopped (), chop (),和 truncate ().

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

这是重载函数。

返回子字符串长度 length 起始于位置 start 在此对象。

注意: 行为未定义当 start < 0, length < 0, or start + length > size ().

另请参阅 left (), right (), chopped (), chop (),和 truncate ().

QStringView::const_reverse_iterator QStringView:: rbegin () const

返回常量 STL-style reverse iterator pointing to the first character in the string, in reverse order.

此函数为兼容 STL (标准模板库) 提供。

另请参阅 rend (), crbegin (),和 begin ().

QStringView::const_reverse_iterator QStringView:: rend () const

返回 STL-style reverse iterator pointing to one past the last character in the string, in reverse order.

此函数为兼容 STL (标准模板库) 提供。

另请参阅 rbegin (), crend (),和 end ().

返回子字符串长度 length 起始于位置 size () - length 在此对象。

注意: 行为未定义当 length < 0 or length > size ().

另请参阅 mid (), left (), chopped (), chop (),和 truncate ().

qsizetype QStringView:: size () const

Returns the size of this string view, in UTF-16 code points (that is, surrogate pairs count as two for the purposes of this function, the same as in QString and QStringRef ).

另请参阅 empty (), isEmpty (), isNull (),和 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.

另请参阅 toUtf8 (), toLocal8Bit (),和 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.

另请参阅 toLatin1 (), toUtf8 (),和 QTextCodec .

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.

警告: QStringView can store strings with more than 2 30 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.

另请参阅 toUtf8 (), toLatin1 (), toLocal8Bit (),和 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 .

另请参阅 toLatin1 (), toLocal8Bit (),和 QTextCodec .

QStringView QStringView:: trimmed () const

Strips leading and trailing whitespace and returns the result.

空白意味着任何字符其。 QChar::isSpace () 返回 true 。这包括 ASCII 字符 \t、\n、\v、\f、\r、及 " "。

void QStringView:: truncate ( qsizetype length )

Truncates this string view to length length .

如同 *this = left(length) .

注意: 行为未定义当 length < 0 or length > size ().

另请参阅 mid (), left (), right (), chopped (),和 chop ().

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

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

storage_type is char16_t .

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

另请参阅 begin (), end (),和 data ().

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

返回字符位于位置 n 在此字符串视图。

行为未定义若 n is negative or not less than size ().

另请参阅 at (), front (),和 back ().

相关非成员

uint qHash ( QStringView key , uint seed = ...)

返回哈希值为 key ,使用 seed 做计算种子。

该函数在 Qt 5.10 引入。