本页内容

QStringView Class

QStringView 类通过QString API 的只读子集,为 UTF-16 字符串提供统一的视图。更多内容...

头文件: #include <QStringView>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意:该类中的所有函数均为可重入的。

公共类型

公共函数

QStringView()
QStringView(const Char (&string)[N])
QStringView(const Char *str)
QStringView(const Container &str)
QStringView(const QString &str)
QStringView(std::nullptr_t)
QStringView(const Char *first, const Char *last)
QStringView(const Char *str, qsizetype len)
QString arg(Args &&... args) const
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(QChar ch) const
int compare(QChar ch, Qt::CaseSensitivity cs) const
int compare(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int compare(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.5) int compare(QUtf8StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.1) QStringView::const_iterator constBegin() const
(since 6.0) QStringView::const_pointer constData() const
(since 6.1) QStringView::const_iterator constEnd() const
bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool contains(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.1) bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
(since 6.1) qsizetype count(const QRegularExpression &re) const
(since 6.0) qsizetype count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.4) qsizetype count(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.0) qsizetype count(QStringView str, 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(QChar ch) const
bool endsWith(QChar ch, Qt::CaseSensitivity cs) const
bool endsWith(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool endsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QChar first() const
(since 6.0) QStringView first(qsizetype n) const
QChar front() const
qsizetype indexOf(QChar ch, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetype indexOf(QLatin1StringView l1, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetype indexOf(QStringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.1) qsizetype indexOf(const QRegularExpression &re, qsizetype from = 0, QRegularExpressionMatch *rmatch = nullptr) const
bool isEmpty() const
(since 6.7) bool isLower() const
bool isNull() const
bool isRightToLeft() const
(since 6.7) bool isUpper() const
bool isValidUtf16() const
QChar last() const
(since 6.0) QStringView last(qsizetype n) const
(since 6.2) qsizetype lastIndexOf(const QRegularExpression &re, QRegularExpressionMatch *rmatch = nullptr) const
qsizetype lastIndexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetype lastIndexOf(QLatin1StringView l1, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetype lastIndexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.1) qsizetype lastIndexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch = nullptr) const
(since 6.3) qsizetype lastIndexOf(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.2) qsizetype lastIndexOf(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.2) qsizetype lastIndexOf(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
qsizetype length() const
(since 6.4) int localeAwareCompare(QStringView other) const
(since 6.8) qsizetype max_size() const
QStringView::const_reverse_iterator rbegin() const
QStringView::const_reverse_iterator rend() const
qsizetype size() const
(since 6.8) QStringView &slice(qsizetype pos, qsizetype n)
(since 6.8) QStringView &slice(qsizetype pos)
(since 6.0) QStringView sliced(qsizetype pos, qsizetype n) const
(since 6.0) QStringView sliced(qsizetype pos) const
(since 6.0) QList<QStringView> split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.0) QList<QStringView> split(QStringView sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.0) QList<QStringView> split(const QRegularExpression &re, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const
bool startsWith(QChar ch) const
bool startsWith(QChar ch, Qt::CaseSensitivity cs) const
bool startsWith(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool startsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
(since 6.0) CFStringRef toCFString() const
(since 6.0) double toDouble(bool *ok = nullptr) const
(since 6.0) float toFloat(bool *ok = nullptr) const
(since 6.0) int toInt(bool *ok = nullptr, int base = 10) const
QByteArray toLatin1() const
QByteArray toLocal8Bit() const
(since 6.0) long toLong(bool *ok = nullptr, int base = 10) const
(since 6.0) qlonglong toLongLong(bool *ok = nullptr, int base = 10) const
(since 6.0) NSString *toNSString() const
(since 6.0) short toShort(bool *ok = nullptr, int base = 10) const
QString toString() const
(since 6.0) uint toUInt(bool *ok = nullptr, int base = 10) const
(since 6.0) ulong toULong(bool *ok = nullptr, int base = 10) const
(since 6.0) qulonglong toULongLong(bool *ok = nullptr, int base = 10) const
(since 6.0) ushort toUShort(bool *ok = nullptr, int base = 10) const
QList<uint> toUcs4() const
QByteArray toUtf8() const
qsizetype toWCharArray(wchar_t *array) const
(since 6.0) auto tokenize(Needle &&sep, Flags... flags) const
QStringView trimmed() const
void truncate(qsizetype length)
const QStringView::storage_type *utf16() const
(since 6.7) operator std::u16string_view() const
QChar operator[](qsizetype n) const

静态公共成员

QStringView fromArray(const Char (&string)[Size])
(since 6.8) qsizetype maxSize()
size_t qHash(QStringView key, size_t seed = 0)
bool operator!=(const QStringView &lhs, const QStringView &rhs)
bool operator<(const QStringView &lhs, const QStringView &rhs)
bool operator<=(const QStringView &lhs, const QStringView &rhs)
bool operator==(const QStringView &lhs, const QStringView &rhs)
bool operator>(const QStringView &lhs, const QStringView &rhs)
bool operator>=(const QStringView &lhs, const QStringView &rhs)

详细说明

QStringView 引用其并不拥有的 UTF-16 字符串中的一段连续区域。它作为所有类型 UTF-16 字符串的接口类型,而无需先构建一个QString 。

该 UTF-16 字符串可以表示为QChar 、ushort 、char16_t 或(在 Windows 等将其视为 16 位类型的平台上)wchar_t 类型的数组(或与数组兼容的数据结构,如QString 、std::basic_string 等)。

QStringView 被设计为一种接口类型;其主要用途是作为函数参数类型。 当 QStringView 用作自动变量或数据成员时,必须确保所引用的字符串数据(例如由QString 拥有的数据)在所有代码路径中都比 QStringView 存活更久,以免字符串视图最终引用已被删除的数据。

当用作接口类型时,QStringView 允许单个函数接受各种各样的 UTF-16 字符串数据源。因此,一个接受 QStringView 的函数可以取代三个函数重载(分别接受QString 和(const QChar*, qsizetype) ),同时还能将更多的字符串数据源传递给该函数,例如u"Hello World" 以及char16_t 字符串字面量。

QStringView 应按值传递,而非按 const 引用传递:

    void myfun1(QStringView sv);        // preferred
    void myfun2(const QStringView &sv); // compiles and works, but slower

若希望为用户提供最大的自由度,使其能够向函数传递任意类型的字符串,请在 QStringView 重载函数中添加以下重载:

  • QChar 的重载:该重载可以委托给 QStringView 版本:
    void fun(QChar ch) { myfun1(QStringView(&ch, 1)); }

    尽管出于技术原因,QStringView 本身无法提供QChar 构造函数。

  • QString:如果你存储的是字符串的未修改副本,因此希望利用QString 的隐式共享特性。
  • QLatin1StringView:如果你可以在不先将QLatin1StringView 转换为UTF-16的情况下实现该函数;用户期望QLatin1StringView 上重载的函数所进行的内存分配,其数量严格少于语义上等价的QStringView版本调用(后者涉及从QLatin1StringView 构建QString )。

QStringView 也可用作函数的返回值。若调用返回 QStringView 的函数,请务必注意:QStringView 的存活时间不得超过该函数承诺保留所引用字符串数据的时间。如有疑问,请通过调用toString() 将 QStringView 转换为QString ,从而获取对数据的强引用。

constexpr QStringView 是一种字面量类型,但由于它将数据存储为char16_t ,因此无法直接进行迭代(将const char16_t* 强制转换为const QChar* ,这在constexpr 函数中是不允许的)。你可以改用带索引的循环和/或在constexpr 上下文中使用utf16()。

另请参阅 “应使用哪个字符串类?”和QString 。

成员类型文档

QStringView::const_iterator

此 typedef 为 `QStringView` 提供了一个 STL 风格的 const 迭代器。

另请参阅 iterator 和const_reverse_iterator 。

QStringView::const_pointer

value_type * 的别名。为兼容 STL 而提供。

QStringView::const_reference

value_type & 的别名。为确保与 STL 的兼容性而提供。

QStringView::const_reverse_iterator

此 typedef 为 `QStringView` 提供了一个 STL 风格的 const 反向迭代器。

另请参阅 reverse_iterator 和const_iterator 。

QStringView::difference_type

std::ptrdiff_t 的别名。为兼容 STL 而提供。

QStringView::iterator

此 typedef 为 `QStringView` 提供了一个 STL 风格的 const 迭代器。

QStringView 由于不支持可变迭代器,因此这与 `const_iterator` 完全相同。

另请参阅 ` const_iterator ` 和 `reverse_iterator`。

QStringView::pointer

value_type * 的别名。为兼容 STL 而提供。

QStringView 不支持可变指针,因此这与const_pointer 效果相同。

QStringView::reference

value_type & 的别名。为兼容 STL 而提供。

QStringView 不支持可变引用,因此这与const_reference 效果相同。

QStringView::reverse_iterator

此 typedef 为 `QStringView` 提供了一个 STL 风格的 const 反向迭代器。

QStringView 由于不支持可变反向迭代器,因此这与 `const_reverse_iterator` 效果相同。

另请参阅 ` const_reverse_iterator ` 和 `iterator`。

QStringView::size_type

qsizetype 的别名。为与 STL 兼容而提供。

QStringView::storage_type

char16_t 的别名。

QStringView::value_type

const QChar 的别名。为与 STL 兼容而提供。

成员函数文档

[constexpr noexcept] QStringView::QStringView()

构建一个空字符串视图。

另请参阅 ` isNull()`。

[constexpr noexcept] template <typename Char, size_t N> QStringView::QStringView(const Char (&string)[N])

基于字符串字面量string 构建一个字符串视图。该视图覆盖该数组,直至遇到第一个Char(0) 或N ,以先出现者为准。若需获取整个数组,请改用fromArray()。

string 必须在该字符串视图对象的生命周期内始终有效。

约束

仅当Char 是兼容的字符类型时,才参与重载解析。兼容的字符类型包括:QChar 、ushort 、char16_t 以及(在 Windows 等将其视为 16 位类型的平台上)wchar_t 。

另请参阅 fromArray 。

[constexpr noexcept] template <typename Char> QStringView::QStringView(const Char *str)

在str 上构建一个字符串视图。其长度由查找第一个Char(0) 的范围决定。

str 在该字符串视图对象的生命周期内,该对象必须始终有效。

将nullptr 作为str 传入是安全的,并将生成一个空字符串视图。

约束

仅当Char 是兼容的字符类型时,才参与重载解析。兼容的字符类型包括:QChar 、ushort 、char16_t 以及(在 Windows 等将其视为 16 位类型的平台上)wchar_t 。

[constexpr noexcept] template <typename Container, QStringView::if_compatible_container<Container> = true> QStringView::QStringView(const Container &str)

在str 上构建一个字符串视图。长度取自std::size(str) 。

std::data(str) 在该字符串视图对象的生命周期内,该长度必须始终有效。

当且仅当std::size(str) == 0 时,字符串视图为空。该构造函数是否会生成空字符串视图未作规定(若要实现此行为,std::data(str) 必须返回nullptr )。

约束

仅当Container 是与value_type 具有兼容字符类型的容器时,才参与重载解析。兼容的字符类型包括:QChar 、ushort 、char16_t 以及(在 Windows 等将其视为 16 位类型的平台上)wchar_t 。

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

[noexcept] QStringView::QStringView(const QString &str)

在str 上构建一个字符串视图。

str.data() 该条件必须在该字符串视图对象的整个生命周期内始终成立。

当且仅当str.isNull() 时,该字符串视图才会为null。

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

构建一个空字符串视图。

另请参阅 ` isNull()`。

[constexpr] template <typename Char, QStringView::if_compatible_char<Char> = true> QStringView::QStringView(const Char *first, const Char *last)

在first 上构建一个字符串视图,其长度为(last -first)。

在该字符串视图对象的生命周期内,[first,last) 范围必须始终有效。

如果last 也是nullptr ,则将\nullptr 作为first 传递是安全的,并将生成一个空字符串视图。

如果last 位于first 之前,或者first 为nullptr 而last 不是,则行为未定义。

约束

仅当Char 是兼容的字符类型时,才会参与重载解析。兼容的字符类型包括:QChar 、ushort 、char16_t 以及(在Windows等将其视为16位类型的平台上)wchar_t 。

[constexpr] template <typename Char, QStringView::if_compatible_char<Char> = true> QStringView::QStringView(const Char *str, qsizetype len)

在str 上构建一个长度为len 的字符串视图。

在该字符串视图对象的生命周期内,范围[str,len) 必须始终有效。

如果len 也是 0,则将nullptr 作为str 传递是安全的,并将生成一个空字符串视图。

如果len 为负数,或者当 为正数且str 等于nullptr 时,行为未定义。

约束

仅当Char 是兼容的字符类型时,才参与重载解析。兼容的字符类型包括:QChar 、ushort 、char16_t 以及(在 Windows 等将其视为 16 位类型的平台上)wchar_t 。

template <typename... Args> QString QStringView::arg(Args &&... args) const

将该字符串中所有%N 替换为args 中的对应参数。这些参数并非按位置对应:args 中的第一个参数将%N 替换为最低的N (所有该值),args 中的第二个参数将%N 替换为次低的N ,以此类推。

Args 可以由任何能隐式转换为QAnyStringView 的内容组成。

注意:在 Qt 6.9 之前的版本中,QAnyStringView 以及 UTF-8 字符串(QUtf8StringView 、QByteArray 、QByteArrayView 、const char8_t* 等)不被支持作为args 。

另请参阅 QString::arg()。

[constexpr noexcept] QChar QStringView::at(qsizetype n) const

返回该字符串视图中位于位置 `n ` 处的字符。

如果n 为负数或不小于size(),则行为未定义。

另请参阅 operator[]()、front() 和back()。

[constexpr] QChar QStringView::back() const

返回字符串视图中的最后一个字符。与last() 相同。

提供此函数是为了兼容 STL。

警告: 对空字符串视图调用 此函数将导致未定义行为。

另请参阅 front()、first() 和last()。

[noexcept] QStringView::const_iterator QStringView::begin() const

返回一个指向字符串视图中第一个字符的常量STL 风格迭代器。

提供此函数是为了兼容 STL。

另请参阅 end()、constBegin()、cbegin()、rbegin() 以及data()。

[noexcept] QStringView::const_iterator QStringView::cbegin() const

与begin()相同。

提供此函数是为了兼容 STL。

另请参阅 cend()、begin()、constBegin()、crbegin() 以及data() 函数。

[noexcept] QStringView::const_iterator QStringView::cend() const

与end()相同。

提供此函数是为了兼容 STL。

另请参阅 cbegin()、end()、constEnd() 以及crend()。

[constexpr noexcept] void QStringView::chop(qsizetype length)

将此字符串视图截断length 个字符。

与 `*this = left(size() - length)` 相同。

注意: 当length < 0 或length >size( ) 时,行为未定义。

另请参阅 sliced()、left()、right()、chopped()、truncate() 以及slice()。

[constexpr noexcept] QStringView QStringView::chopped(qsizetype length) const

返回从该对象开头开始,长度为 `size()` - `length ` 的子字符串。

与 `left(size() - length)` 相同。

注意: 当length < 0 或length >size() 时,行为未定义。

另请参阅 sliced()、left()、right()、chop()、truncate() 以及slice()。

[constexpr noexcept] int QStringView::compare(QChar ch) const

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

[noexcept] int QStringView::compare(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

将此字符串视图与 Latin-1 字符串视图l1 或字符ch 进行比较。如果此字符串视图小于l1 或ch ,则返回负整数;如果大于l1 或ch ,则返回正整数;如果两者相等,则返回零。

如果 `cs ` 的值为 `Qt::CaseSensitive `(默认值),则比较区分大小写;否则,比较不区分大小写。

另请参阅 operator==()、operator<() 和operator>()。

[noexcept] int QStringView::compare(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

将此字符串视图与字符串视图str 进行比较:如果此字符串视图小于str ,则返回一个负整数;如果大于str ,则返回一个正整数;如果两者相等,则返回零。

如果cs 等于Qt::CaseSensitive (默认值),则比较区分大小写;否则,比较不区分大小写。

另请参阅 operator==()、operator<() 和operator>()。

[noexcept, since 6.5] int QStringView::compare(QUtf8StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

将此字符串视图与QUtf8StringView str 进行比较:如果此字符串视图小于str ,则返回负整数;如果大于str ,则返回正整数;如果两者相等,则返回零。

如果cs 为Qt::CaseSensitive (默认值),则比较区分大小写;否则,比较不区分大小写。

该函数在 Qt 6.5 中引入。

另请参阅 operator==()、operator<( ) 和operator>( )。

[noexcept, since 6.1] QStringView::const_iterator QStringView::constBegin() const

与begin() 相同。

该函数在 Qt 6.1 中引入。

另请参阅 constEnd()、begin()、cbegin()、crbegin() 以及data()。

[noexcept, since 6.0] QStringView::const_pointer QStringView::constData() const

返回指向字符串视图中第一个字符的 const 指针。

注意: 返回值所表示的字符数组并非以空字符结尾。

该函数在 Qt 6.0 中引入。

另请参阅 data()、begin()、end() 和utf16()。

[noexcept, since 6.1] QStringView::const_iterator QStringView::constEnd() const

与end() 相同。

该函数在 Qt 6.1 中引入。

另请参阅 constBegin()、end()、cend() 以及crend()。

[noexcept] bool QStringView::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[noexcept] bool QStringView::contains(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[noexcept] bool QStringView::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

如果该字符串视图中包含由str 表示的 UTF-16 字符串、由l1 表示的 Latin-1 字符串,或由ch 表示的字符,则返回true ;否则返回false 。

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

另请参阅 indexOf()。

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

如果正则表达式re 与该字符串视图中的某处匹配,则返回true ;否则返回false 。

如果匹配成功且rmatch 不是nullptr ,它还会将匹配结果写入由rmatch 指向的QRegularExpressionMatch 对象中。

该函数在 Qt 6.1 中引入。

另请参阅 QRegularExpression::match()。

[since 6.1] qsizetype QStringView::count(const QRegularExpression &re) const

返回正则表达式re 在字符串视图中匹配的次数。

出于历史原因,该函数会统计重叠的匹配项。这种行为与使用 `QRegularExpressionMatchIterator` 遍历字符串视图中的匹配项有所不同。

该函数于 Qt 6.1 版本中引入。

另请参阅 QRegularExpression::globalMatch()。

[noexcept, since 6.0] qsizetype QStringView::count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

返回字符ch 在字符串视图中出现的次数。

如果 `cs ` 为 `Qt::CaseSensitive `(默认值),则搜索区分大小写;否则,搜索不区分大小写。

此函数重载了QStringView::count()。

该函数在 Qt 6.0 中引入。

另请参阅 QString::count()、contains() 和indexOf()。

[since 6.4] qsizetype QStringView::count(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

返回在该字符串视图中,l1 识别出的 Latin-1 字符串的出现次数(可能存在重叠)。

如果 `cs ` 为 `Qt::CaseSensitive `(默认值),则搜索区分大小写;否则,搜索不区分大小写。

此函数重载了QStringView::count()。

该函数在 Qt 6.4 中引入。

另请参阅 QString::count()、contains() 和indexOf()。

[noexcept, since 6.0] qsizetype QStringView::count(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

返回字符串视图中字符串视图str 的出现次数(可能存在重叠)。

如果 `cs ` 为 `Qt::CaseSensitive `(默认值),则搜索区分大小写;否则,搜索不区分大小写。

此函数重载了QStringView::count()。

该函数在 Qt 6.0 中引入。

另请参阅 QString::count()、contains() 和indexOf()。

[noexcept] QStringView::const_reverse_iterator QStringView::crbegin() const

与 `rbegin()` 相同。

提供此函数是为了兼容 STL。

另请参阅 crend()、rbegin() 和cbegin()。

[noexcept] QStringView::const_reverse_iterator QStringView::crend() const

与rend() 相同。

提供此函数是为了兼容 STL。

另请参阅 crbegin()、rend() 和cend()。

[noexcept] QStringView::const_pointer QStringView::data() const

返回指向字符串视图中第一个字符的 const 指针。

注意: 返回值所表示的字符数组不是以空字符结尾的。

另请参阅 begin()、end() 和utf16()。

[constexpr noexcept] bool QStringView::empty() const

返回此字符串视图是否为空——即size() == 0 。

提供此函数是为了兼容 STL。

另请参阅 isEmpty()、isNull()、size() 和length()。

[noexcept] QStringView::const_iterator QStringView::end() const

返回一个指向列表中最后一个字符之后那个虚拟字符的 constSTL 风格迭代器。

提供此函数是为了兼容 STL。

另请参阅 begin()、constEnd()、cend() 以及rend()。

[noexcept] bool QStringView::endsWith(QChar ch) const

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

[noexcept] bool QStringView::endsWith(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

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

如果该字符串视图以str 所表示的 UTF-16 字符串、l1 所表示的 Latin-1 字符串,或ch 所表示的字符结尾,则返回true ;否则返回false 。

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

另请参阅 startsWith()。

[constexpr] QChar QStringView::first() const

返回字符串视图中的第一个字符。与front() 功能相同。

提供此函数是为了与其他 Qt 容器保持兼容性。

警告: 对空字符串视图调用 此函数将导致未定义行为。

另请参阅 front()、back() 和last()。

[constexpr noexcept, since 6.0] QStringView QStringView::first(qsizetype n) const

返回一个字符串视图,该视图指向此字符串视图的前n 个字符。

注意: 当n < 0 或n >size() 时,行为未定义。

该函数自 Qt 6.0 起引入。

另请参阅 last()、sliced()、startsWith()、chopped()、chop()、truncate() 以及slice()。

[static constexpr noexcept] template < typename Char, size_t Size, QStringView::if_compatible_char<Char> = true > QStringView QStringView::fromArray(const Char (&string)[Size])

基于完整的字符字符串字面量string (包含Size 个元素,包括任何尾随的Char(0) )构建一个字符串视图。如果你不希望将空终止符包含在视图中,可以在确定它位于末尾时通过chop()将其移除。 或者,您可以使用接受数组字面量的构造函数重载,该构造函数将创建一个视图,其范围截止于数据中的第一个空终止符(但不包含该空终止符)。

string 在该字符串视图对象的生命周期内必须保持有效。

如果 `Char ` 是兼容的字符类型,则此函数可与任何数组字面量配合使用。兼容的字符类型包括:QChar 、ushort 、char16_t 以及(在 Windows 等将其视为 16 位类型的平台上)wchar_t 。

[constexpr] QChar QStringView::front() const

返回字符串视图中的第一个字符。与first() 相同。

提供此函数是为了兼容 STL。

警告: 对空字符串视图调用 此函数将导致未定义行为。

另请参见 back()、first() 和last()。

[noexcept] qsizetype QStringView::indexOf(QChar ch, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[noexcept] qsizetype QStringView::indexOf(QLatin1StringView l1, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[noexcept] qsizetype QStringView::indexOf(QStringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

返回在此字符串视图中,从索引位置from 开始向前搜索,UTF-16 字符串(由str 表示)、Latin-1 字符串(由l1 表示)或字符ch 首次出现的位置索引。如果分别未找到str 、l1 或ch ,则返回 -1。

如果cs 的值为Qt::CaseSensitive (默认值),则搜索区分大小写;否则,搜索不区分大小写。

如果from 为 -1,则从最后一个字符开始搜索;如果为 -2,则从倒数第二个字符开始搜索,以此类推。

另请参阅 QString::indexOf()。

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

返回正则表达式re 在字符串视图中首次匹配的位置索引,从索引位置from 开始向前搜索。如果re 在任何位置均未匹配,则返回 -1。

如果匹配成功且rmatch 并非nullptr ,该函数还会将匹配结果写入由rmatch 指向的QRegularExpressionMatch 对象中。

注意:由于 正则表达式匹配算法的运作方式,该函数实际上会从字符串视图的开头开始反复进行匹配,直到到达位置from 为止。

该函数于 Qt 6.1 中引入。

[constexpr noexcept] bool QStringView::isEmpty() const

返回该字符串视图是否为空——即size() == 0 。

提供此函数是为了与其他 Qt 容器保持兼容性。

另请参阅 empty()、isNull()、size() 和length()。

[noexcept, since 6.7] bool QStringView::isLower() const

如果该视图与其小写折叠形式完全相同,则返回true 。

请注意,这并不意味着字符串视图中不包含大写字母(某些大写字母没有小写折叠形式;toString().toLower() 会将其保持不变)。有关更多信息,请参阅 Unicode 标准第 3.13 节。

该函数自 Qt 6.7 起引入。

另请参阅 QChar::toLower() 和isUpper()。

[constexpr noexcept] bool QStringView::isNull() const

返回该字符串视图是否为空——即data() == nullptr 。

提供此函数是为了与其他 Qt 容器保持兼容性。

另请参见 empty()、isEmpty()、size() 和length()。

[noexcept] bool QStringView::isRightToLeft() const

如果字符串视图是从右向左读取的,则返回true 。

另请参阅 QString::isRightToLeft()。

[noexcept, since 6.7] bool QStringView::isUpper() const

如果该视图与其大写转换形式完全相同,则返回true 。

请注意,这并不意味着该字符串视图不包含小写字母(某些小写字母没有大写折叠形式;它们在调用toString().toUpper() 时保持不变)。有关更多信息,请参阅 Unicode 标准第 3.13 节。

该函数在 Qt 6.7 中引入。

另请参阅 QChar::toUpper() 和isLower()。

[noexcept] bool QStringView::isValidUtf16() const

如果字符串视图包含有效的 UTF-16 编码数据,则返回true ;否则返回false 。

请注意,此函数不会对数据进行任何特殊验证;它仅检查数据是否能从 UTF-16 成功解码。数据被假定为采用主机字节序;BOM 的存在不影响结果。

另请参阅 QString::isValidUtf16()。

[constexpr] QChar QStringView::last() const

返回字符串视图中的最后一个字符。与back() 相同。

提供此函数是为了与其他 Qt 容器保持兼容性。

警告: 对空字符串视图调用 此函数将导致未定义行为。

另请参见 back()、front() 和first()。

[constexpr noexcept, since 6.0] QStringView QStringView::last(qsizetype n) const

返回一个字符串视图,该视图指向此字符串视图中最后n 个字符。

注意: 当n < 0 或n >size() 时,行为未定义。

该函数在 Qt 6.0 中引入。

另请参阅 first()、sliced()、endsWith()、chopped()、chop()、truncate() 和slice()。

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

返回正则表达式re 在字符串视图中的最后一个匹配位置。如果re 未在任何位置匹配,则返回 -1。

如果匹配成功且rmatch 不是nullptr ,它还会将匹配结果写入由rmatch 指向的QRegularExpressionMatch 对象中。

注意:由于 正则表达式匹配算法的运作方式,该函数实际上会从字符串视图的开头开始反复进行匹配,直到到达字符串视图的结尾为止。

该函数在 Qt 6.2 中引入。

[noexcept] qsizetype QStringView::lastIndexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[noexcept] qsizetype QStringView::lastIndexOf(QLatin1StringView l1, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[noexcept] qsizetype QStringView::lastIndexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

返回在此字符串视图中,UTF-16字符串(由str 处理)、Latin-1字符串(由l1 处理)或字符ch 的最后一次出现位置的索引,从索引位置from 开始向后搜索。

如果from 为 -1,则从最后一个字符开始搜索;如果为 -2,则从倒数第二个字符开始搜索,以此类推。

如果分别未找到str 、l1 或ch ,则返回 -1。

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

注意:当 搜索长度为 0 的str 或l1时, 即使通常认为-1 是从字符串视图的末尾开始搜索的,但通过负值from 仍会将数据末尾的匹配项从搜索中排除:末尾的匹配项位于最后一个字符之后,因此会被排除。 若要包含此类末尾的空匹配,可为from 指定正值,或完全省略from 参数。

另请参阅 QString::lastIndexOf()。

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

返回正则表达式re 在字符串视图中的最后一个匹配项的索引位置,该匹配项始于索引位置from 之前。

如果from 为 -1,则从最后一个字符开始搜索;如果为 -2,则从倒数第二个字符开始,以此类推。

如果re 在任何位置均未匹配,则返回 -1。

如果匹配成功,且rmatch 不是nullptr ,它还会将匹配结果写入由rmatch 所指向的QRegularExpressionMatch 对象中。

注意:由于 正则表达式匹配算法的运作方式,该函数实际上会从字符串视图的开头开始反复进行匹配,直到到达位置from 为止。

注意:当 搜索可能匹配 0 个字符的正则表达式re 时, 即使通常认为-1 是从字符串视图的末尾开始搜索的,但通过负值from 仍会将数据末尾的匹配结果排除在搜索之外:末尾的匹配位于最后一个字符之后,因此会被排除。 若要包含此类最后的空匹配,可为from 指定正值,或完全省略from 参数。

该函数在 Qt 6.1 中引入。

[noexcept, since 6.3] qsizetype QStringView::lastIndexOf(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

该函数重载了QStringView::lastIndexOf()。

该函数在 Qt 6.3 中引入。

[noexcept, since 6.2] qsizetype QStringView::lastIndexOf(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

[noexcept, since 6.2] qsizetype QStringView::lastIndexOf(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

返回该字符串视图中,由str 所识别的UTF-16字符串或由l1 所识别的Latin-1字符串的最后一次出现位置的索引,该搜索从该字符串视图的最后一个字符开始向后进行。如果未找到str 或l1 ,则分别返回-1。

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

这些函数在 Qt 6.2 中引入。

另请参阅 QString::lastIndexOf()。

[constexpr noexcept] qsizetype QStringView::length() const

与size() 相同。

提供此函数是为了与其他 Qt 容器保持兼容性。

另请参阅 empty()、isEmpty()、isNull() 以及size()。

[since 6.4] int QStringView::localeAwareCompare(QStringView other) const

将此字符串视图与other 字符串视图进行比较,并根据此字符串视图是否小于、等于或大于other 字符串视图,返回小于、等于或大于零的整数。

比较操作会根据区域设置和平台进行。使用此函数可向用户展示按顺序排序的字符串列表。

该函数在 Qt 6.4 中引入。

另请参阅 Comparing Strings 。

[static constexpr noexcept, since 6.8] qsizetype QStringView::maxSize()

它返回视图理论上能够呈现的元素最大数量。实际上,该数量可能要小得多,具体取决于系统可用的内存容量。

该函数于 Qt 6.8 版本中引入。

[constexpr noexcept, since 6.8] qsizetype QStringView::max_size() const

提供此函数是为了兼容 STL。

返回maxSize()。

该函数在 Qt 6.8 中引入。

[noexcept] QStringView::const_reverse_iterator QStringView::rbegin() const

返回一个指向字符串视图中第一个字符(按反向顺序)的 constSTL 风格反向迭代器。

提供此函数是为了兼容 STL。

另请参阅 rend()、crbegin() 和begin()。

[noexcept] QStringView::const_reverse_iterator QStringView::rend() const

返回一个STL 风格的反向迭代器,该迭代器指向字符串视图中最后一个字符之后的一个位置,且顺序为反向。

提供此函数是为了兼容 STL。

另请参见 rbegin()、crend() 和end()。

[constexpr noexcept] qsizetype QStringView::size() const

返回此字符串视图的大小,单位为 UTF-16 码单元(也就是说,就本函数而言,代理对计为两个,这与QString 中的一致)。

另请参阅 empty()、isEmpty()、isNull() 以及length()。

[constexpr, since 6.8] QStringView &QStringView::slice(qsizetype pos, qsizetype n)

将此字符串视图修改为从位置pos 开始,并向后延伸n 个码点。

注意: 当pos < 0、n < 0 或pos +n >size() 时,行为 未定义。

该函数在 Qt 6.8 中引入。

另请参阅 sliced()、first()、last()、chopped()、chop() 以及truncate()。

[constexpr, since 6.8] QStringView &QStringView::slice(qsizetype pos)

将此字符串视图修改为从位置pos 开始,并延伸至其末尾。

注意: 当pos < 0 或pos >size() 时,行为未定义。

这是一个重载函数。

该函数在 Qt 6.8 中引入。

另请参阅 sliced()、first()、last()、chopped()、chop() 以及truncate()。

[constexpr noexcept, since 6.0] QStringView QStringView::sliced(qsizetype pos, qsizetype n) const

返回一个字符串视图,该视图指向此字符串视图中从位置pos 开始的n 个字符。

注意: 当pos < 0、n < 0 或pos +n >size() 时,行为 未定义。

该函数自 Qt 6.0 起引入。

另请参阅 first()、last()、chopped()、chop()、truncate() 和slice()。

[constexpr noexcept, since 6.0] QStringView QStringView::sliced(qsizetype pos) const

返回一个字符串视图,该视图从该对象的pos 位置开始,一直延伸到其末尾。

注意: 当pos < 0 或pos >size() 时,行为未定义。

这是一个重载函数。

该函数在 Qt 6.0 中引入。

另请参阅 first()、last()、chopped()、chop()、truncate() 以及slice()。

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

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

在遇到sep 时,将视图拆分为子字符串视图,并返回这些字符串视图的列表。

有关 `sep`、`behavior ` 和 `cs ` 如何协同作用形成结果,请参阅 `QString::split()`。

注意: 只要该字符串视图所引用的数据有效,所有 返回的视图均 有效。销毁该数据将导致所有视图变为悬空视图。

这些函数在 Qt 6.0 中引入。

[since 6.0] QList<QStringView> QStringView::split(const QRegularExpression &re, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const

在正则表达式re 匹配的位置将字符串拆分为子字符串视图,并返回这些字符串的列表。如果re 在字符串中没有任何匹配,则 split() 会返回一个单元素列表,其中包含该字符串作为视图。

注意: 返回列表中的视图是该视图的子视图;因此,它们引用与该视图相同的数据,并且仅在该数据仍处于活动状态时才有效。

这是一个重载函数。

该函数在 Qt 6.0 中引入。

[noexcept] bool QStringView::startsWith(QChar ch) const

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

[noexcept] bool QStringView::startsWith(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

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

如果该字符串视图以str 所表示的UTF-16字符串、l1 所表示的Latin-1字符串,或ch 所表示的字符开头,则返回true ;否则返回false 。

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

另请参阅 endsWith()。

[since 6.0] CFStringRef QStringView::toCFString() const

根据此QStringView 创建一个CFString。

调用方拥有该 CFString,并负责释放它。

注意:此 函数仅在 macOS 和 iOS 上可用。

该函数于 Qt 6.0 中引入。

[since 6.0] double QStringView::toDouble(bool *ok = nullptr) const

返回已转换为double 值的字符串视图。

如果转换发生溢出,则返回无穷大;如果因其他原因(例如下溢)导致转换失败,则返回 0.0。

如果ok 不是nullptr ,则通过将*ok 设置为false 来报告失败,通过将*ok 设置为true 来报告成功。

字符串转换始终在“C”语言环境下进行。若需进行与语言环境相关的转换,请使用QLocale::toDouble()

出于历史原因,此函数不处理千位分隔符。若需转换此类数字,请使用QLocale::toDouble()。

该函数于 Qt 6.0 中引入。

另请参阅 QString::toDouble()。

[since 6.0] float QStringView::toFloat(bool *ok = nullptr) const

返回已转换为float 值的字符串视图。

如果转换发生溢出,则返回无穷大;如果因其他原因(例如下溢)导致转换失败,则返回 0.0。

如果ok 不是nullptr ,则通过将*ok 设置为false 来报告失败,通过将*ok 设置为true 来报告成功。

字符串转换始终在“C”区域设置下进行。若需进行依赖区域设置的转换,请使用QLocale::toFloat()

该函数在 Qt 6.0 中引入。

另请参阅 QString::toFloat()。

[since 6.0] int QStringView::toInt(bool *ok = nullptr, int base = 10) const

返回已转换为int 格式的字符串视图,转换时使用基数base (默认值为10,必须介于2到36之间,或为0)。如果转换失败,则返回0。

如果ok 不是nullptr ,则通过将*ok 设置为false 来报告失败,通过将*ok 设置为true 来报告成功。

如果base 为 0,则采用 C 语言约定:如果字符串视图以 "0x" 开头,则使用 16 进制;否则,如果字符串视图以 "0" 开头,则使用 8 进制;否则,使用 10 进制。

字符串转换始终在“C”区域设置下进行。若需进行与区域设置相关的转换,请使用QLocale::toInt()

该函数在 Qt 6.0 中引入。

另请参阅 QString::toInt()。

QByteArray QStringView::toLatin1() const

返回字符串的Latin-1表示形式,格式为QByteArray 。

如果字符串包含非Latin-1字符,则行为未定义。

另请参阅 toUtf8()、toLocal8Bit() 和QStringEncoder 。

QByteArray QStringView::toLocal8Bit() const

返回字符串的本地 8 位表示形式,类型为 `QByteArray`。

在 Unix 系统上,这等同于toUtf8();在 Windows 上,则使用系统当前的代码页。

如果字符串包含本地化环境 8 位编码不支持的字符,则行为未定义。

另请参阅 toLatin1()、toUtf8() 和QStringEncoder 。

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

返回使用基数base (默认值为10,必须在2到36之间,或为0)转换为long 的字符串视图。如果转换失败,则返回0。

如果 `ok ` 不是 `nullptr`,则通过将 `*ok ` 设置为 `false` 来报告失败,通过将 `*ok ` 设置为 `true` 来报告成功。

如果base 为 0,则采用 C 语言约定:如果字符串视图以 "0x" 开头,则使用十六进制;否则,如果字符串视图以 "0" 开头,则使用八进制;否则,使用十进制。

字符串转换始终在“C”区域设置下进行。若需进行与区域设置相关的转换,请使用QLocale::toLong()

该函数在 Qt 6.0 中引入。

另请参阅 QString::toLong()。

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

返回使用基准值base 将字符串视图转换为long long 的结果,该基准值默认为 10,必须在 2 到 36 之间,或为 0。如果转换失败,则返回 0。

如果 `ok ` 不是 `nullptr`,则通过将 `*ok ` 设置为 `false` 来报告失败,通过将 `*ok ` 设置为 `true` 来报告成功。

如果base 为 0,则采用 C 语言约定:如果字符串视图以 "0x" 开头,则使用 16 进制;否则,如果字符串视图以 "0" 开头,则使用 8 进制;否则,使用 10 进制。

字符串转换始终在 'C' 区域设置下进行。若需进行与区域设置相关的转换,请使用QLocale::toLongLong()

该函数在 Qt 6.0 中引入。

另请参阅 QString::toLongLong()。

[since 6.0] NSString *QStringView::toNSString() const

根据此QStringView 创建一个NSString。

该 NSString 对象将自动释放。

注意:此 函数仅在 macOS 和 iOS 上可用。

该函数在 Qt 6.0 中引入。

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

返回使用基数base (默认值为10,必须介于2到36之间,或为0)转换为short 格式的字符串视图。如果转换失败,则返回0。

如果ok 不是nullptr ,则通过将*ok 设置为false 来报告失败,通过将*ok 设置为true 来报告成功。

如果base 为 0,则采用 C 语言约定:如果字符串视图以 "0x" 开头,则使用 16 进制;否则,如果字符串视图以 "0" 开头,则使用 8 进制;否则,使用 10 进制。

字符串转换始终在“C”区域设置下进行。若需进行与区域设置相关的转换,请使用QLocale::toShort()

该函数在 Qt 6.0 中引入。

另请参阅 QString::toShort()。

QString QStringView::toString() const

返回该字符串视图数据的深度拷贝,类型为QString 。

当且仅当该字符串视图为 null 时,返回值才会是空的 `QString `。

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

返回使用基数base (默认值为10,必须在2到36之间,或为0)转换为unsigned int 格式的字符串视图。如果转换失败,则返回0。

如果ok 不是nullptr ,则通过将*ok 设置为false 来报告失败,通过将*ok 设置为true 来报告成功。

如果base 为 0,则采用 C 语言约定:如果字符串视图以 "0x" 开头,则采用 16 进制;否则,如果字符串视图以 "0" 开头,则采用 8 进制;否则,采用 10 进制。

字符串转换始终在“C”区域设置下进行。若需进行与区域设置相关的转换,请使用QLocale::toUInt()

该函数在 Qt 6.0 中引入。

另请参阅 QString::toUInt()。

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

返回使用基数base (默认值为10,必须在2到36之间,或为0)转换为unsigned long 的字符串视图。如果转换失败,则返回0。

如果ok 不是nullptr ,则通过将*ok 设置为false 来报告失败,通过将*ok 设置为true 来报告成功。

如果base 为 0,则采用 C 语言约定:如果字符串视图以 "0x" 开头,则使用十六进制;否则,如果字符串视图以 "0" 开头,则使用八进制;否则,使用十进制。

字符串转换始终在“C”区域设置下进行。若需进行与区域设置相关的转换,请使用QLocale::toULongLong()

该函数在 Qt 6.0 中引入。

另请参阅 QString::toULong()。

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

返回使用基数base (默认值为10,必须在2到36之间,或为0)转换为unsigned long long 的字符串视图。如果转换失败,则返回0。

如果 `ok ` 不是 `nullptr`,则通过将 `*ok ` 设置为 `false` 来报告失败,通过将 `*ok ` 设置为 `true` 来报告成功。

如果base 为 0,则采用 C 语言约定:如果字符串视图以 "0x" 开头,则使用十六进制;否则,如果字符串视图以 "0" 开头,则使用八进制;否则,使用十进制。

字符串转换始终在“C”区域设置下进行。若需进行与区域设置相关的转换,请使用QLocale::toULongLong()

该函数自 Qt 6.0 起引入。

另请参阅 QString::toULongLong()。

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

返回使用基数base (默认值为10,必须在2到36之间,或为0)转换为unsigned short 的字符串视图。如果转换失败,则返回0。

如果 `ok ` 不是 `nullptr`,则通过将 `*ok ` 设置为 `false` 来报告失败,通过将 `*ok ` 设置为 `true` 来报告成功。

如果base 为 0,则采用 C 语言约定:如果字符串视图以 "0x" 开头,则采用 16 进制;否则,如果字符串视图以 "0" 开头,则采用 8 进制;否则,采用 10 进制。

字符串转换始终在“C”区域设置下进行。若需进行与区域设置相关的转换,请使用QLocale::toUShort()

该函数在 Qt 6.0 中引入。

另请参阅 QString::toUShort()。

QList<uint> QStringView::toUcs4() const

返回字符串视图的 UCS-4/UTF-32 表示形式,类型为QList<uint>。

UCS-4 是一种 Unicode 编码,因此属于无损编码。该字符串视图中的所有字符都将采用 UCS-4 进行编码。该字符串视图中任何无效的代码单元序列都将被替换为 Unicode 替换字符(QChar::ReplacementCharacter ,对应于U+FFFD )。

返回的列表不是以 0 结尾的。

另请参阅 toUtf8()、toLatin1()、toLocal8Bit() 以及QStringEncoder 。

QByteArray QStringView::toUtf8() const

返回字符串视图的 UTF-8 表示形式,即QByteArray 。

UTF-8 是一种 Unicode 编码方案,能够表示 Unicode 字符串中的所有字符,例如QString 。

另请参阅 toLatin1()、toLocal8Bit() 和QStringEncoder 。

qsizetype QStringView::toWCharArray(wchar_t *array) const

将此字符串视图转换为给定的array 。

调用方需确保array 的容量足够容纳该字符串视图的wchar_t 编码(分配一个长度与字符串视图相同的数组通常已足够)。 在wchar_t 为2字节宽的平台(例如Windows)上,该数组采用UTF-16编码;否则(Unix系统),假定wchar_t 为4字节宽,数据采用UCS-4编码写入。

注意:此 函数不会在array 的末尾写入空终止符。

返回写入array 的wchar_t 条目数量。

另请参阅 QString::toWCharArray()。

[constexpr noexcept(...), since 6.0] template <typename Needle, typename... Flags> auto QStringView::tokenize(Needle &&sep, Flags... flags) const

在遇到sep 的位置将字符串拆分为子字符串视图,并返回这些字符串的懒加载序列。

等同于

return QStringTokenizer{std::forward<Needle>(sep), flags...};

,不同之处在于它无需在编译器中启用 C++17 类模板参数推导 (CTAD) 即可运行。

有关 `QStringTokenizer ` 中 `sep ` 和 `flags ` 如何协同作用以形成结果,请参见 。

注意:虽然 此函数返回QStringTokenizer ,但绝、对、不、要显式命名其模板参数。如果可以使用 C++17 类模板参数推导(CTAD),则可以编写

QStringTokenizer result = sv.tokenize(sep);

(无需模板参数)。若无法使用 C++17 CTAD,则必须仅将返回值存储在 `auto ` 变量中:

auto result = sv.tokenize(sep);

这是因为QStringTokenizer 的模板参数与返回它们的具体tokenize()重载之间存在非常微妙的依赖关系,而且这些参数通常与分隔符所用的类型并不对应。

这些函数在 Qt 6.0 中引入。

注意: 当 `noexcept(qTokenize(std::declval<const QStringView&>(), std::forward<Needle>(needle), flags...)) ` 为 `true()` 重载时,该函数 为 `noexcept`。

另请参阅 QStringTokenizer 和qTokenize()。

[noexcept] QStringView QStringView::trimmed() const

去除首尾空格,并返回结果。

空格指任何使QChar::isSpace()返回true 的字符。这包括ASCII字符'\t'、'\n'、'\v'、'\f'、'\r'以及' '。

[constexpr noexcept] void QStringView::truncate(qsizetype length)

将此字符串视图截断为长度length 。

与*this = left(length) 相同。

注意: 当length < 0 或length >size() 时,行为未定义。

另请参阅 sliced()、left()、right()、chopped() 以及chop()。

[constexpr noexcept] const QStringView::storage_type *QStringView::utf16() const

返回指向字符串视图中第一个字符的 const 指针。

注意: 返回值所表示的字符数组并非以空字符结尾。

storage_type 即 `char16_t`。

另请参阅 begin()、end() 和data()。

[constexpr noexcept, since 6.7] QStringView::operator std::u16string_view() const

将此QStringView 对象转换为std::u16string_view 对象。返回的视图将具有与该视图相同的数据指针和长度。

该函数自 Qt 6.7 起引入。

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

返回该字符串视图中位置为n 处的字符。

如果n 为负数或不小于size(),则行为未定义。

另请参阅 at()、front() 和back()。

相关的非成员

[noexcept] size_t qHash(QStringView key, size_t seed = 0)

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

[noexcept] bool operator!=(const QStringView &lhs, const QStringView &rhs)

[noexcept] bool operator<(const QStringView &lhs, const QStringView &rhs)

[noexcept] bool operator<=(const QStringView &lhs, const QStringView &rhs)

[noexcept] bool operator==(const QStringView &lhs, const QStringView &rhs)

[noexcept] bool operator>(const QStringView &lhs, const QStringView &rhs)

[noexcept] bool operator>=(const QStringView &lhs, const QStringView &rhs)

用于比较lhs 与rhs 的运算符。

另请参阅 compare()。

© 2026 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.