QStringView Class
QStringView 类通过QString API 的只读子集为 UTF-16 字符串提供了统一的视图。更多
Header: | #include <QStringView> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
- 所有成员(包括继承成员)的列表
- 已废弃成员
- QStringView 属于字符串数据类。
注意:该类中的所有函数都是可重入的。
公共类型
const_iterator | |
const_pointer | |
const_reference | |
const_reverse_iterator | |
difference_type | |
iterator | |
pointer | |
reference | |
reverse_iterator | |
size_type | |
storage_type | |
value_type |
公共函数
QStringView() | |
QStringView(const Char (&)[N] string) | |
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 c, 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 c, 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 c, 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 c, 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) std::u16string_view | operator std::u16string_view() const |
QChar | operator[](qsizetype n) const |
静态公共成员
相关非成员
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) { fun(QStringView(&ch, 1)); }
尽管由于技术原因,QStringView 自身无法提供QChar 构造函数。
- QString:如果你存储了一个未修改的字符串副本,因此想利用QString 的隐式共享。
- QLatin1StringView QLatin1StringView 用户希望在QLatin1StringView 上重载的函数比调用 QStringView 版本的语义等同函数(涉及从QLatin1StringView 构建QString )执行更少的内存分配。
QStringView 也可用作函数的返回值。如果你调用一个返回 QStringView 的函数,请格外小心,不要让 QStringView 的存在时间超过函数承诺的保持引用字符串数据存活的时间。如果有疑问,可调用toString() 将 QStringView 转换为QString ,从而获得对数据的强引用。
QStringView 是一种字面类型(Literal Type),但由于它以char16_t
的形式存储数据,因此迭代不是constexpr
(从const char16_t*
转换为const QChar*
,这在constexpr
函数中是不允许的)。您可以在constexpr
上下文中使用索引循环和/或utf16() 来代替。
另请参阅 QString 。
成员类型文档
QStringView::const_iterator
该类型定义为QStringView 提供了 STL 风格的常量迭代器。
另请参见 iterator 和const_reverse_iterator 。
QStringView::const_pointer
value_type *
的别名。与 STL 兼容。
QStringView::const_reference
value_type &
的别名。与 STL 兼容。
QStringView::const_reverse_iterator
该类型定义为QStringView 提供了 STL 风格的 const 反向迭代器。
另请参见 reverse_iterator 和const_iterator 。
QStringView::difference_type
std::ptrdiff_t
的别名。与 STL 兼容。
QStringView::iterator
该类型定义为QStringView 提供了 STL 风格的常量迭代器。
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
该类型定义为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 兼容而提供。
成员函数文档
[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 中引入。
template <typename... Args> QString QStringView::arg(Args &&... args) const
用args 中的相应参数替换该字符串中出现的%N
。参数不分位置:args 的第一个参数用最低的N
(全部)替换%N
,args 的第二个参数用次低的N
替换%N
,等等。
Args
可以由隐式转换为 的任何内容组成。QAnyStringView
注意: 在 6.9 之前的 Qt XML 版本中,QAnyStringView 和 UTF-8 字符串(QUtf8StringView,QByteArray,QByteArrayView,const char8_t*
等)不支持args 。
另请参阅 QString::arg() 。
[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 ,则返回一个正整数;如果两者相等,则返回 0。
如果cs 是Qt::CaseSensitive (默认值),比较将区分大小写;否则比较将不区分大小写。
另请参阅 operator==()、operator<() 和operator>()。
[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() 。
[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() 。
[noexcept]
qsizetype QStringView::indexOf(QChar c, 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 开始向前搜索,分别返回str 查看的 UTF-16 字符串、l1 查看的 Latin-1 字符串或ch 字符在该字符串视图中首次出现的索引位置。如果未找到str 、l1 或ch ,则分别返回-1。
如果cs 是Qt::CaseSensitive (默认值),则搜索区分大小写;否则搜索不区分大小写。
如果from 为-1,则从最后一个字符开始搜索;如果为-2,则从次最后一个字符开始,依此类推。
另请参阅 QString::indexOf() 。
[noexcept]
bool QStringView::contains(QChar c, 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() 。
[noexcept]
qsizetype QStringView::lastIndexOf(QChar c, 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
从索引位置from 开始向后搜索,分别返回str 查看的 UTF-16 字符串、l1 查看的 Latin-1 字符串或字符ch 在该字符串视图中最后出现的索引位置。
如果from 为-1,则从最后一个字符开始搜索;如果为-2,则从次最后一个字符开始搜索,依此类推。
如果未找到str 、l1 或c ,则分别返回-1。
如果cs 是Qt::CaseSensitive (默认值),则搜索区分大小写;否则搜索不区分大小写。
注意: 当搜索长度为 0 的str 或l1 时,数据末尾的匹配将通过负from 从搜索中排除,尽管-1
通常被认为是从字符串视图的末尾开始搜索:末尾的匹配在最后一个字符之后,因此被排除在外。要包含这种最后的空匹配,要么为from 赋正值,要么完全省略from 参数。
另请参见 QString::lastIndexOf().
[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
此函数重载lastIndexOf()。
从本字符串视图的最后一个字符开始向后搜索,返回本字符串视图中最后出现的 UTF-16 字符串的索引位置(分别为str 查看的 UTF-16 字符串或l1 查看的 Latin-1 字符串)。如果未找到str 或l1 ,则分别返回-1。
如果cs 是Qt::CaseSensitive (默认值),则搜索区分大小写;否则搜索不区分大小写。
此函数在 Qt 6.2 中引入。
另请参阅 QString::lastIndexOf()。
[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)的情况下工作。
关于sep 和flags 如何交互形成结果,请参见QStringTokenizer 。
注意: 虽然此函数返回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()。
[constexpr noexcept]
QStringView::QStringView()
构造空字符串视图。
另请参阅 isNull()。
[constexpr noexcept]
template <typename Char, size_t N> QStringView::QStringView(const Char (&)[N] string)
在字符串字面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
。
[noexcept]
QStringView::QStringView(const QString &str)
在str 上构造一个字符串视图。
str.data()
必须在该字符串视图对象的生命周期内保持有效。
当且仅当str.isNull()
.
[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
,则以first 的形式传递\nullptr
是安全的,但会导致字符串视图为空。
如果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
。
[constexpr noexcept]
QChar QStringView::at(qsizetype n) const
返回该字符串视图中位于n 位置的字符。
如果n 为负数或不小于size(),则行为未定义。
另请参阅 operator[]()、front() 和back()。
[constexpr]
QChar QStringView::back() const
返回字符串视图中的最后一个字符。与last() 相同。
提供此函数是为了与 STL 兼容。
警告: 在空字符串视图中调用此函数会导致未定义的行为。
[noexcept]
QStringView::const_iterator QStringView::begin() const
返回指向字符串视图中第一个字符的 ConstSTL 样式迭代器。
提供此函数是为了与 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() 时,行为未定义。
另请参阅 mid()、left()、right()、chopped()、truncate() 和slice()。
[constexpr noexcept]
QStringView QStringView::chopped(qsizetype length) const
返回从此对象开始的长度为size() -length 的子字符串。
与left(size() - length)
相同。
注意: 当length < 0 或length >size() 时,行为未定义。
另请参阅 mid()、left()、right()、chop()、truncate() 和slice()。
[noexcept]
int QStringView::compare(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
将此字符串视图与字符串视图str 进行比较,如果此字符串视图小于str ,则返回一个负整数;如果大于str ,则返回一个正整数;如果两者相等,则返回 0。
如果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 ,则返回正整数;如果两者相等,则返回 0。
如果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
返回指向字符串视图中第一个字符的常量指针。
注意: 返回值所代表的字符数组不是空端。
此函数在 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()。
[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
此函数重载 count()。
返回字符ch 在字符串视图中出现的次数。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
此函数在 Qt 6.0 中引入。
另请参阅 QString::count()、contains() 和indexOf()。
[since 6.4]
qsizetype QStringView::count(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 count()。
返回l1 在此字符串视图中查看的 Latin-1 字符串的(可能重叠的)出现次数。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
此函数在 Qt 6.4 中引入。
另请参阅 QString::count()、contains() 和indexOf()。
[noexcept, since 6.0]
qsizetype QStringView::count(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 count()。
返回字符串视图str 在该字符串视图中出现的次数(可能重叠)。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
此函数在 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
返回指向字符串视图中第一个字符的常量指针。
注意: 返回值所代表的字符数组不是空端。
[constexpr noexcept]
bool QStringView::empty() const
返回该字符串视图是否为空,即size() == 0
是否为空。
提供此函数是为了与 STL 兼容。
另请参阅 isEmpty()、isNull()、size() 和length() 。
[noexcept]
QStringView::const_iterator QStringView::end() const
返回一个常STL 风格的迭代器,指向列表中最后一个字符之后的虚数字符。
提供此函数是为了与 STL 兼容。
另请参阅 begin()、constEnd()、cend() 和rend()。
[constexpr]
QChar QStringView::first() const
返回字符串视图中的第一个字符。与front() 相同。
提供此函数是为了与其他 Qt 容器兼容。
警告: 在空字符串视图上调用此函数会导致未定义的行为。
[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 (&)[Size] string)
在完整字符串字面string 上构建字符串视图,包括任何尾部Char(0)
。如果不想在视图中包含 null 终结符,可以在确定它位于末尾时将其chop() 掉。另外,您也可以使用构造函数重载数组字面量,这样就可以创建一个视图,直到但不包括数据中的第一个空结束符。
string 必须在该字符串视图对象的生命周期内保持有效。
如果Char
是兼容字符类型,则该函数可用于任何数组字面量。兼容字符类型包括QChar
,ushort
,char16_t
以及(在诸如 Windows 等平台上为 16 位类型)wchar_t
。
[constexpr]
QChar QStringView::front() const
返回字符串视图中的第一个字符。与first() 相同。
提供此函数是为了与 STL 兼容。
警告: 在空字符串视图上调用此函数会导致未定义的行为。
[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 容器兼容。
警告: 在空字符串视图上调用此函数会导致未定义的行为。
[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 中引入。
[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 时,数据末尾的匹配将通过负from 从搜索中排除,尽管-1
通常被认为是从字符串视图的末尾开始搜索:末尾的匹配在最后一个字符之后,因此被排除在外。要包含这种最后的空匹配,要么为from 赋一个正值,要么完全省略from 参数。
此函数在 Qt 6.1 中引入。
[noexcept, since 6.3]
qsizetype QStringView::lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载了 lastIndexOf()。
此函数在 Qt 6.3 中引入。
[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 兼容。
[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
返回指向n 字符的字符串视图,从pos 位置开始。
注意: 当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(const QRegularExpression &re, Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const
这是一个重载函数。
只要正则表达式re 匹配,它就会将字符串分割成子串视图,并返回这些字符串的列表。如果re 在字符串中没有匹配到,split() 将返回一个单元素列表,其中包含该字符串视图。
注意: 返回列表中的视图是该视图的子视图;因此,它们引用了与该视图相同的数据,并在该数据仍然有效期间保持有效。
此函数在 Qt 6.0 中引入。
[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
返回使用base 为基数转换为int
的字符串视图,基数默认为 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
以QByteArray 的形式返回字符串的 Latin-1 表示形式。
如果字符串包含非 Latin1 字符,则行为未定义。
另请参阅 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 的基数将字符串视图转换为long
,默认为 10,且必须介于 2 和 36 之间,否则返回 0。如果转换失败,则返回 0。
如果ok 不是nullptr
,则通过将 *ok 设置为false
来报告失败,通过将 *ok 设置为true
来报告成功。
如果base 为 0,则使用 C 语言惯例:如果字符串视图以 "0x "开头,则使用基数 16;否则,如果字符串视图以 "0 "开头,则使用基数 8;否则,使用基数 10。
字符串转换总是在 "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 的基数将字符串视图转换为short
,默认为 10,且必须介于 2 和 36 之间,否则返回 0。如果转换失败,则返回 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 的形式返回该字符串视图数据的深度副本。
如果且仅当该字符串视图为空,返回值将是空QString 。
[since 6.0]
uint QStringView::toUInt(bool *ok = nullptr, int base = 10) const
返回使用base 为基数转换为unsigned int
的字符串视图,基数默认为 10,且必须介于 2 和 36 之间,否则返回 0。如果转换失败,则返回 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 为基数转换为unsigned long
的字符串视图,基数默认为 10,且必须介于 2 和 36 之间,否则返回 0。如果转换失败,则返回 0。
如果ok 不是nullptr
,则通过将 *ok 设置为false
来报告失败,通过将 *ok 设置为true
来报告成功。
如果base 为 0,则使用 C 语言惯例:如果字符串视图以 "0x "开头,则使用基数 16;否则,如果字符串视图以 "0 "开头,则使用基数 8;否则,使用基数 10。
字符串转换总是在 "C "语言中进行。要进行与本地语言相关的转换,请使用QLocale::toULongLong()
该函数在 Qt 6.0 中引入。
另请参阅 QString::toULong()。
[since 6.0]
qulonglong QStringView::toULongLong(bool *ok = nullptr, int base = 10) const
返回使用base 为基数转换为unsigned long long
的字符串视图,基数默认为 10,且必须介于 2 和 36 之间,否则返回 0。如果转换失败,则返回 0。
如果ok 不是nullptr
,则通过将 *ok 设置为false
来报告失败,通过将 *ok 设置为true
来报告成功。
如果base 为 0,则使用 C 语言惯例:如果字符串视图以 "0x "开头,则使用基数 16;否则,如果字符串视图以 "0 "开头,则使用基数 8;否则,使用基数 10。
字符串转换总是在 "C "语言中进行。要进行与本地语言相关的转换,请使用QLocale::toULongLong()
该函数在 Qt 6.0 中引入。
另请参阅 QString::toULongLong()。
[since 6.0]
ushort QStringView::toUShort(bool *ok = nullptr, int base = 10) const
返回使用base 为基数转换为unsigned short
的字符串视图,基数默认为 10,且必须介于 2 和 36 之间,否则返回 0。如果转换失败,则返回 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
以QByteArray 的形式返回字符串视图的 UTF-8 表示形式。
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()。
[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() 时,行为未定义。
另请参阅 mid(),left(),right(),chopped() 和chop() 。
[constexpr noexcept]
const QStringView::storage_type *QStringView::utf16() const
返回指向字符串视图中第一个字符的常量指针。
注意: 返回值所代表的字符数组不是空端。
storage_type
是 。char16_t
[noexcept, since 6.7]
std::u16string_view QStringView::operator std::u16string_view() const
将QStringView 对象转换为std::u16string_view
对象。返回的视图将具有与此视图相同的数据指针和长度。
此函数在 Qt 6.7 中引入。
[constexpr]
QChar QStringView::operator[](qsizetype n) const
返回该字符串视图中位于n 位置的字符。
如果n 为负数或不小于size(),则行为未定义。
相关非成员
[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().
[noexcept]
size_t qHash(QStringView key, size_t seed = 0)
返回key 的哈希值,使用seed 作为计算的种子。
© 2025 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.