QStringRef Class
QStringRef 类为QString 子串提供了一个薄包装。更多
Header: | #include <QStringRef> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core5Compat) target_link_libraries(mytarget PRIVATE Qt6::Core5Compat) |
qmake: | QT += core5compat |
- 所有成员(包括继承成员)的列表
- QStringRef 属于字符串数据类。
注意:该类中的所有函数都是可重入的。
公共类型
公共函数
QStringRef() | |
QStringRef(const QString *string) | |
QStringRef(const QString *string, int position, int length) | |
QStringRef(const QStringRef &other) | |
~QStringRef() | |
QStringRef | appendTo(QString *string) const |
const QChar | at(int position) const |
QChar | back() const |
QStringRef::const_iterator | begin() const |
QStringRef::const_iterator | cbegin() const |
QStringRef::const_iterator | cend() const |
void | chop(int n) |
QStringRef | chopped(int len) const |
void | clear() |
int | compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | compare(const QByteArray &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QStringRef::const_iterator | constBegin() const |
const QChar * | constData() const |
QStringRef::const_iterator | constEnd() const |
bool | contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | count() const |
int | count(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | count(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QStringRef::const_reverse_iterator | crbegin() const |
QStringRef::const_reverse_iterator | crend() const |
const QChar * | data() const |
QStringRef::const_iterator | end() const |
bool | endsWith(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | endsWith(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | endsWith(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | endsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | endsWith(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QChar | front() const |
int | indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | indexOf(QStringView str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | isEmpty() const |
bool | isNull() const |
bool | isRightToLeft() const |
int | lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | lastIndexOf(QStringView str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
int | lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QStringRef | left(int n) const |
int | length() const |
int | localeAwareCompare(const QString &other) const |
int | localeAwareCompare(const QStringRef &other) const |
QStringRef | mid(int position, int n = -1) const |
int | position() const |
QStringRef::const_reverse_iterator | rbegin() const |
QStringRef::const_reverse_iterator | rend() const |
QStringRef | right(int n) const |
int | size() const |
QList<QStringRef> | split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QList<QStringRef> | split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | startsWith(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | startsWith(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | startsWith(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | startsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | startsWith(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
const QString * | string() const |
double | toDouble(bool *ok = nullptr) const |
float | toFloat(bool *ok = nullptr) const |
int | toInt(bool *ok = nullptr, int base = 10) const |
QByteArray | toLatin1() const |
QByteArray | toLocal8Bit() const |
long | toLong(bool *ok = nullptr, int base = 10) const |
qlonglong | toLongLong(bool *ok = nullptr, int base = 10) const |
short | toShort(bool *ok = nullptr, int base = 10) const |
QString | toString() const |
uint | toUInt(bool *ok = nullptr, int base = 10) const |
ulong | toULong(bool *ok = nullptr, int base = 10) const |
qulonglong | toULongLong(bool *ok = nullptr, int base = 10) const |
ushort | toUShort(bool *ok = nullptr, int base = 10) const |
QList<uint> | toUcs4() const |
QByteArray | toUtf8() const |
QStringRef | trimmed() const |
void | truncate(int position) |
const QChar * | unicode() const |
bool | operator!=(const char *s) const |
bool | operator<(const char *s) const |
bool | operator<=(const char *s) const |
QStringRef & | operator=(const QString *string) |
QStringRef & | operator=(const QStringRef &other) |
bool | operator==(const char *s) const |
bool | operator>(const char *s) const |
bool | operator>=(const char *s) const |
QChar | operator[](int position) const |
静态公共成员
int | compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
int | compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
int | compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
int | localeAwareCompare(const QStringRef &s1, const QString &s2) |
int | localeAwareCompare(const QStringRef &s1, const QStringRef &s2) |
相关非成员
bool | operator<(const QStringRef &s1, const QStringRef &s2) |
bool | operator<=(const QStringRef &s1, const QStringRef &s2) |
bool | operator==(QLatin1String s1, const QStringRef &s2) |
bool | operator==(const QString &s1, const QStringRef &s2) |
bool | operator==(const QStringRef &s1, const QStringRef &s2) |
bool | operator>(const QStringRef &s1, const QStringRef &s2) |
bool | operator>=(const QStringRef &s1, const QStringRef &s2) |
详细说明
QStringRef 提供了QString API 的只读子集。
字符串引用明确引用了string() 中的一部分,该部分具有给定的size(),从特定的position() 开始。调用toString() 会返回一个数据副本,即一个真正的QString 实例。
该类旨在提高处理从现有QString 实例获取的子串时的性能。QStringRef 通过简单地引用原始字符串的一部分,避免了标准QString 的内存分配和引用计数开销。在低级代码(如解析器中使用的代码)中,这可能被证明是有利的,但代价可能是更复杂的代码。
对于大多数用户来说,使用 QStringRef 而不是QString 在语义上没有任何好处,因为 QStringRef 需要注意内存管理问题,可能会使代码的编写和维护变得更加复杂。
警告: 只要引用的字符串存在,QStringRef 就有效。如果原始字符串被删除,则字符串引用指向的内存位置无效。
我们建议您只在稳定的代码中使用该类,因为通过剖析可以清楚地发现,使用该类提供的优化子字符串处理方法取代标准字符串操作可以提高性能。
另请参阅 " 隐式共享类"。
成员类型文档
QStringRef::const_iterator
另请参见 QStringRef::const_reverse_iterator 。
QStringRef::const_reverse_iterator
另请参见 QStringRef::const_iterator 。
成员函数文档
[constexpr noexcept]
QStringRef::QStringRef()
构造一个空字符串引用。
QStringRef::QStringRef(const QString *string)
为给定的string 构建字符串引用。
QStringRef::QStringRef(const QString *string, int position, int length)
构造一个字符串引用,指向string 中的字符范围,该字符范围由position 和length 起始字符数指定。
警告: 此函数旨在尽可能提高性能,不执行边界检查。为保证程序的正确性,position 和length 必须描述string 的有效子串。
这意味着起始position 必须为正数或 0,且小于string 的长度;length 必须为正数或 0,且小于字符串长度减去起始position ;即 0 <= position <= string->length() 和 0 <= length <= string->length() - position 必须同时满足。
[noexcept default]
QStringRef::QStringRef(const QStringRef &other)
构建other 字符串引用的副本。
[noexcept default]
QStringRef::~QStringRef()
销毁字符串引用。
由于该类仅用于引用字符串数据,并不拥有其所有权,因此销毁实例时不会释放内存。
QStringRef QStringRef::appendTo(QString *string) const
将字符串引用追加到string ,并返回一个指向合并字符串数据的新引用。
const QChar QStringRef::at(int position) const
返回字符串引用中给定索引position 处的字符。
position 必须是字符串中的有效索引位置(即 0 <=position <size() )。
QChar QStringRef::back() const
返回字符串中的最后一个字符。与at(size() - 1)
相同。
提供此函数是为了与 STL 兼容。
警告: 在空字符串上调用此函数会导致未定义的行为。
另请参阅 front()、at() 和operator[]()。
QStringRef::const_iterator QStringRef::begin() const
返回指向字符串中第一个字符的 ConstSTL 样式迭代器。
另请参阅 cbegin()、constBegin()、end()、constEnd()、rbegin() 和rend()。
QStringRef::const_iterator QStringRef::cbegin() const
与begin() 相同。
另请参阅 begin(),constBegin(),cend(),constEnd(),rbegin() 和rend().
QStringRef::const_iterator QStringRef::cend() const
与end() 相同。
另请参阅 end(),constEnd(),cbegin(),constBegin(),rbegin() 和rend().
[noexcept]
void QStringRef::chop(int n)
从字符串末尾删除n 字符。
如果n 大于或等于size() ,则结果为空字符串;如果n 为负数,则相当于传递 0。
另请参阅 QString::chop() 和truncate()。
QStringRef QStringRef::chopped(int len) const
返回该字符串最左端size() -len 字符的子串引用。
注意: 如果len 为负数或大于size(),则行为未定义。
另请参阅 endsWith()、left()、right()、mid()、chop() 和truncate()。
void QStringRef::clear()
清除字符串引用的内容,使其为空。
[static noexcept]
int QStringRef::compare(const QStringRef &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
将s1 与s2 进行比较,如果s1 小于s2 ,则返回负整数;如果大于s2 ,则返回正整数;如果两者相等,则返回 0。
如果cs 是Qt::CaseSensitive ,则比较区分大小写;否则比较不区分大小写。
[noexcept]
int QStringRef::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
这是一个重载函数。
将*this
与ch 进行比较,如果*this
小于ch ,则返回负整数;如果大于ch ,则返回正整数;如果两者相等,则返回 0。这里,ch 解释为长度为 1 的字符串。
如果cs 是Qt::CaseSensitive ,则比较区分大小写;否则比较不区分大小写。
[noexcept]
int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
这是一个重载函数。
将*this
与other 进行比较,如果*this
小于other ,则返回负整数;如果大于other ,则返回正整数;如果两者相等,则返回 0。
如果cs 是Qt::CaseSensitive ,则比较区分大小写;否则比较不区分大小写。
等价于compare(*this, other, cs)
。
int QStringRef::compare(const QByteArray &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
这是一个重载函数。
将*this
与other 进行比较,如果*this
小于other ,则返回负整数;如果大于other ,则返回正整数;如果两者相等,则返回 0。other 的内容被解释为 UTF-8。
如果cs 是Qt::CaseSensitive ,则比较区分大小写;否则比较不区分大小写。
等同于compare(*this, other, cs)
。
[noexcept]
int QStringRef::compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
这是一个重载函数。
将*this
与other 进行比较,如果*this
小于other ,则返回负整数;如果大于other ,则返回正整数;如果两者相等,则返回 0。
如果cs 是Qt::CaseSensitive ,则比较区分大小写;否则比较不区分大小写。
等价于compare(*this, other, cs)
。
[noexcept]
int QStringRef::compare(const QStringRef &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
这是一个重载函数。
将*this
与other 进行比较,如果*this
小于other ,则返回负整数;如果大于other ,则返回正整数;如果两者相等,则返回 0。
如果cs 是Qt::CaseSensitive ,则比较区分大小写;否则比较不区分大小写。
等价于compare(*this, other, cs)
。
[static noexcept]
int QStringRef::compare(const QStringRef &s1, QLatin1String s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
这是一个重载函数。
将s1 与s2 进行比较,如果s1 小于s2 ,则返回负整数;如果大于s2 ,则返回正整数;如果两者相等,则返回 0。
如果cs 是Qt::CaseSensitive ,则比较区分大小写;否则比较不区分大小写。
[static noexcept]
int QStringRef::compare(const QStringRef &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
这是一个重载函数。
将s1 与s2 进行比较,如果s1 小于s2 ,则返回负整数;如果大于s2 ,则返回正整数;如果两者相等,则返回 0。
如果cs 是Qt::CaseSensitive ,则比较区分大小写;否则比较不区分大小写。
QStringRef::const_iterator QStringRef::constBegin() const
与begin() 相同。
另请参见 begin(),cend(),constEnd(),rbegin() 和rend().
const QChar *QStringRef::constData() const
与unicode() 相同。
QStringRef::const_iterator QStringRef::constEnd() const
与end() 相同。
另请参阅 end(),cend(),cbegin(),constBegin(),rbegin() 和rend().
bool QStringRef::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
如果该字符串引用包含字符串str 的出现,则返回true
;否则返回false
。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
bool QStringRef::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 contains()。
如果该字符串包含字符ch 的出现,则返回true
;否则返回false
。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
bool QStringRef::contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 contains()。
如果该字符串引用包含字符串str 的出现,则返回true
;否则返回false
。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
[noexcept]
bool QStringRef::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 contains()。
如果该字符串引用包含字符串视图str 的出现,则返回true
;否则返回false
。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
bool QStringRef::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 contains()。
如果该字符串引用包含字符串引用str 的出现,则返回true
;否则返回false
。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
int QStringRef::count() const
返回字符串引用的字符数。等同于size() 和length()。
int QStringRef::count(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
返回str 字符串在该字符串引用中出现的次数(可能重叠)。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::count()、contains() 和indexOf()。
int QStringRef::count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 count()。
返回字符ch 在字符串引用中出现的次数。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::count()、contains() 和indexOf()。
int QStringRef::count(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 count()。
返回字符串引用str 在该字符串引用中出现的次数(可能重叠)。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参见 QString::count()、contains() 和indexOf()。
QStringRef::const_reverse_iterator QStringRef::crbegin() const
与rbegin() 相同。
另请参阅 begin(),rbegin() 和rend().
QStringRef::const_reverse_iterator QStringRef::crend() const
与rend() 相同。
const QChar *QStringRef::data() const
与unicode() 相同。
QStringRef::const_iterator QStringRef::end() const
返回一个常STL 样式的迭代器,指向列表中最后一个字符之后的虚数字符。
另请参见 cbegin()、constBegin()、end()、constEnd()、rbegin() 和rend()。
bool QStringRef::endsWith(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
如果字符串引用以str 结尾,则返回true
;否则返回false
。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::endsWith() 和startsWith()。
bool QStringRef::endsWith(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 endsWith()。
如果字符串引用以ch 结尾,则返回true
;否则返回false
。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::endsWith() 和endsWith()。
bool QStringRef::endsWith(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载了 endsWith()。
另请参阅 QString::endsWith() 和endsWith()。
[noexcept]
bool QStringRef::endsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载了 endsWith()。
另请参阅 QString::endsWith() 和startsWith()。
bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载了 endsWith()。
另请参阅 QString::endsWith() 和endsWith()。
QChar QStringRef::front() const
返回字符串中的第一个字符。与at(0)
相同。
提供此函数是为了与 STL 兼容。
警告: 在空字符串上调用此函数会导致未定义的行为。
另请参阅 back()、at() 和operator[]()。
int QStringRef::indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
从索引位置from 开始向前搜索,返回str 字符串在此字符串引用中首次出现的索引位置。如果未找到str ,则返回-1。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
如果from 为-1,则从最后一个字符开始搜索;如果为-2,则从次最后一个字符开始,依此类推。
另请参见 QString::indexOf()、lastIndexOf()、contains() 和count()。
int QStringRef::indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
从索引位置from 开始向前搜索,返回str 字符串在此字符串引用中首次出现的索引位置。如果未找到str ,则返回-1。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
如果from 为-1,则从最后一个字符开始搜索;如果为-2,则从次最后一个字符开始,依此类推。
另请参见 QString::indexOf()、lastIndexOf()、contains() 和count()。
int QStringRef::indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载 indexOf()。
从索引位置from 开始向前搜索,返回字符ch 在字符串引用中首次出现的索引位置。如果找不到ch ,则返回-1。
另请参阅 QString::indexOf()、lastIndexOf()、contains() 和count()。
[noexcept]
int QStringRef::indexOf(QStringView str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载 indexOf()。
从索引位置from 开始向前搜索,返回该字符串引用中首次出现的字符串视图str 的索引位置。如果未找到str ,则返回-1。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
如果from 为-1,则从最后一个字符开始搜索;如果为-2,则从次最后一个字符开始,依此类推。
另请参见 QString::indexOf()、QStringView::indexOf()、lastIndexOf()、contains() 和count()。
int QStringRef::indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载 indexOf()。
从索引位置from 开始向前搜索,返回该字符串引用str 第一次出现的索引位置。如果未找到str ,则返回-1。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::indexOf()、lastIndexOf()、contains() 和count()。
bool QStringRef::isEmpty() const
如果字符串引用没有字符,则返回true
;否则返回false
。
如果字符串引用的大小为零,则该字符串引用为空。
另请参见 size()。
bool QStringRef::isNull() const
如果该字符串引用没有引用字符串,或者引用的字符串为空(即QString::isNull() 为 true),则返回true
。
另请参见 size()。
bool QStringRef::isRightToLeft() const
如果字符串从右向左读取,则返回true
。
另请参阅 QString::isRightToLeft().
int QStringRef::lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
从索引位置from 开始向后搜索,返回该字符串引用中最后出现的字符串str 的索引位置。如果from 为 -1(默认),则从最后一个字符开始搜索;如果from 为 -2,则从次最后一个字符开始搜索,依此类推。如果未找到str ,则返回 -1。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::lastIndexOf()、indexOf()、contains() 和count()。
int QStringRef::lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载了 lastIndexOf()。
返回字符ch 最后出现的索引位置,从位置from 开始向后搜索。
另请参见 QString::lastIndexOf()、indexOf()、contains() 和count()。
int QStringRef::lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 lastIndexOf()。
从索引位置from 开始向后搜索,返回该字符串引用中最后出现的字符串str 的索引位置。如果from 为 -1(默认),则从最后一个字符开始搜索;如果from 为 -2,则从次最后一个字符开始搜索,依此类推。如果未找到str ,则返回 -1。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::lastIndexOf()、indexOf()、contains() 和count()。
[noexcept]
int QStringRef::lastIndexOf(QStringView str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 lastIndexOf()。
返回字符串视图str 在该字符串中最后出现的索引位置,从索引位置from 开始向后搜索。如果from 为 -1(默认),则从最后一个字符开始搜索;如果from 为 -2,则从次最后一个字符开始搜索,依此类推。如果未找到str ,则返回 -1。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 indexOf()、contains() 和count()。
int QStringRef::lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载 lastIndexOf()。
从索引位置from 开始向后搜索,返回该字符串引用str 最后出现的索引位置。如果from 为 -1(默认),则从最后一个字符开始搜索;如果from 为 -2,则从次最后一个字符开始搜索,依此类推。如果未找到str ,则返回 -1。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::lastIndexOf()、indexOf()、contains() 和count()。
QStringRef QStringRef::left(int n) const
返回n 字符串最左端字符的子串引用。
如果n 大于或等于size() 或小于零,则返回整个字符串的引用。
另请参阅 right()、mid()、startsWith()、chopped()、chop() 和truncate()。
int QStringRef::length() const
返回字符串引用的字符数。等同于size() 和count()。
[static]
int QStringRef::localeAwareCompare(const QStringRef &s1, const QString &s2)
将s1 与s2 进行比较,如果s1 小于s2 ,则返回负整数;如果大于s2 ,则返回正整数;如果两者相等,则返回 0。
比较的方式取决于本地和平台。使用此函数可向用户显示已排序的字符串列表。
另请参阅 compare()、QLocale 和Comparing Strings 。
int QStringRef::localeAwareCompare(const QString &other) const
这是一个重载函数。
将*this
与other 进行比较,如果*this
小于other ,则返回负整数;如果大于other ,则返回正整数;如果两者相等,则返回 0。
比较的方式取决于本地和平台。使用此函数可向用户显示已排序的字符串列表。
另请参见 Comparing Strings 。
int QStringRef::localeAwareCompare(const QStringRef &other) const
这是一个重载函数。
将*this
与other 进行比较,如果*this
小于other ,则返回负整数;如果大于other ,则返回正整数;如果两者相等,则返回 0。
比较的方式取决于本地和平台。使用此函数可向用户显示已排序的字符串列表。
另请参见 Comparing Strings 。
[static]
int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef &s2)
这是一个重载函数。
将s1 与s2 进行比较,如果s1 小于s2 ,则返回负整数;如果大于s2 ,则返回正整数;如果两者相等,则返回 0。
比较的方式取决于本地和平台。使用此函数可向用户显示已排序的字符串列表。
另请参见 Comparing Strings 。
QStringRef QStringRef::mid(int position, int n = -1) const
从指定的position 开始,返回该字符串中n 字符的子串引用。
如果position 超过字符串长度,则返回空引用。
如果字符串中的可用字符少于n ,从给定的position 开始,或者n 为-1(默认值),函数将返回从指定的position 开始的所有字符。
另请参阅 left()、right()、chopped()、chop() 和truncate()。
int QStringRef::position() const
返回字符串引用所指字符串的起始位置。
QStringRef::const_reverse_iterator QStringRef::rbegin() const
按相反顺序返回指向字符串中第一个字符的 ConstSTL 样式反向迭代器。
另请参阅 begin()、crbegin() 和rend()。
QStringRef::const_reverse_iterator QStringRef::rend() const
返回一个STL 样式的反向迭代器,按相反顺序指向字符串中最后一个字符之后的一个字符。
QStringRef QStringRef::right(int n) const
返回n 字符串最右边字符的子串引用。
如果n 大于或等于size() 或小于零,则返回整个字符串的引用。
另请参阅 left()、mid()、endsWith()、chopped()、chop() 和truncate()。
int QStringRef::size() const
返回字符串引用的字符数。等同于length() 和count()。
QList<QStringRef> QStringRef::split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
将sep 出现的字符串拆分成子串引用,并返回这些字符串的列表。
关于sep 、behavior 和cs 如何相互作用形成结果,请参阅QString::split() 。
注意: 只要该字符串有效,所有引用都有效。销毁此字符串将导致所有引用成为悬空指针。
QList<QStringRef> QStringRef::split(QChar sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
这是一个重载函数。
bool QStringRef::startsWith(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
如果字符串引用以str 开头,则返回true
;否则返回false
。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::startsWith() 和endsWith()。
bool QStringRef::startsWith(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
此函数重载 startsWith()。
如果字符串引用以ch 开始,则返回true
;否则返回false
。
如果cs 是Qt::CaseSensitive (默认),则搜索区分大小写;否则搜索不区分大小写。
另请参阅 QString::startsWith() 和endsWith()。
bool QStringRef::startsWith(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载 startsWith()。
另请参阅 QString::startsWith() 和endsWith()。
[noexcept]
bool QStringRef::startsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载 startsWith()。
另请参阅 QString::startsWith() 和endsWith()。
bool QStringRef::startsWith(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
该函数重载 startsWith()。
另请参阅 QString::startsWith() 和endsWith()。
const QString *QStringRef::string() const
返回字符串引用指向的字符串指针,如果没有指向字符串,则返回 0。
另请参见 unicode()。
double QStringRef::toDouble(bool *ok = nullptr) const
返回转换为double
值的字符串。
如果转换溢出,则返回无穷大;如果转换因其他原因(如下溢)失败,则返回 0.0。
如果ok 不是nullptr
,则通过将 *ok 设置为false
来报告失败,通过将 *ok 设置为true
来报告成功。
字符串转换将始终在 "C "语言中进行。要进行与本地语言相关的转换,请使用QLocale::toDouble()
由于历史原因,此函数不处理千位分隔符。如果需要转换此类数字,请使用QLocale::toDouble()。
另请参见 QString::toDouble()。
float QStringRef::toFloat(bool *ok = nullptr) const
返回转换为float
值的字符串。
如果转换溢出,则返回无穷大;如果转换因其他原因(如下溢)失败,则返回 0.0。
如果ok 不是nullptr
,则通过将 *ok 设置为false
来报告失败,通过将 *ok 设置为true
来报告成功。
字符串转换将始终在 "C "语言中进行。要进行与语言有关的转换,请使用QLocale::toFloat()
另请参见 QString::toFloat()。
int QStringRef::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()
另请参见 QString::toInt()。
QByteArray QStringRef::toLatin1() const
以QByteArray 的形式返回字符串的 Latin-1 表示形式。
如果字符串包含非 Latin1 字符,则返回的字节数组未定义。这些字符可能会被抑制或用问号代替。
另请参阅 toUtf8()、toLocal8Bit() 和QStringEncoder 。
QByteArray QStringRef::toLocal8Bit() const
返回字符串的本地 8 位表示形式QByteArray 。如果字符串包含本地 8 位编码不支持的字符,则返回的字节数组未定义。
在 Unix 系统中,这等同于toUtf8();在 Windows 系统中,则使用系统当前的代码页。
如果字符串中包含任何无法在本地编码的字符,返回的字节数组将是未定义的。这些字符可能会被抑制或替换为其他字符。
另请参阅 toLatin1(),toUtf8() 和QStringEncoder 。
long QStringRef::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()
另请参见 QString::toLong()。
qlonglong QStringRef::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()
另请参见 QString::toLongLong()。
short QStringRef::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()
另请参见 QString::toShort()。
QString QStringRef::toString() const
以QString 对象形式返回字符串引用的副本。
如果字符串引用不是字符串的完整引用(即position() 为 0 且size() 等于string()->size() ),则此函数将分配一个新字符串返回。
另请参见 string()。
uint QStringRef::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()
另请参见 QString::toUInt()。
ulong QStringRef::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()
另请参见 QString::toULong()。
qulonglong QStringRef::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()
另请参见 QString::toULongLong()。
ushort QStringRef::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()
另请参见 QString::toUShort()。
QList<uint> QStringRef::toUcs4() const
返回字符串的 UCS-4/UTF-32 表示形式,即QList<uint>。
UCS-4 是一种 Unicode 编解码器,因此是无损的。该字符串中的所有字符都将以 UCS-4 编码。字符串中任何无效的编码单位序列都将被 Unicode 替换字符(QChar::ReplacementCharacter ,对应于U+FFFD
)替换。
返回的列表不以 0'结束。
另请参阅 toUtf8(),toLatin1(),toLocal8Bit() 和QStringEncoder 。
QByteArray QStringRef::toUtf8() const
以QByteArray 的形式返回字符串的 UTF-8 表示形式。
UTF-8 是一种 Unicode 编解码器,可以表示 Unicode 字符串中的所有字符,如QString 。
另请参阅 toLatin1(),toLocal8Bit() 和QStringEncoder 。
QStringRef QStringRef::trimmed() const
返回删除了首尾空白的字符串。
空白指QChar::isSpace() 返回true
的任何字符。这包括 ASCII 字符'\t'、'\n'、'\v'、'\f'、'\r'和''。
与QString::simplified() 不同,trimmed() 只保留内部空白。
另请参见 QString::trimmed().
[noexcept]
void QStringRef::truncate(int position)
在给定的position 索引处截断字符串。
如果指定的position 索引超出了字符串的末尾,则什么也不会发生。
如果position 为负数,则相当于传递 0。
另请参阅 QString::truncate() 。
const QChar *QStringRef::unicode() const
返回字符串引用的 Unicode 表示形式。由于数据直接源于引用字符串,因此除非字符串引用包含字符串的空结束符,否则不会以 0 结尾。
另请参见 string().
bool QStringRef::operator!=(const char *s) const
该函数重载了 operator!=()。
s 常量字符指针使用 fromUtf8() 函数转换为QStringRef 。
您可以在编译应用程序时通过定义QT_NO_CAST_FROM_ASCII
来禁用该操作符。例如,如果您想确保所有用户可见字符串都通过QObject::tr() 转换,这将非常有用。
如果该字符串与参数字符串s 在词法上不相等,则返回true
。否则返回false
。
另请参阅 QT_NO_CAST_FROM_ASCII 。
bool QStringRef::operator<(const char *s) const
该函数重载了 operator<()。
s 常量字符指针使用 fromUtf8() 函数转换为QStringRef 。
您可以在编译应用程序时通过定义QT_NO_CAST_FROM_ASCII
来禁用此操作符。例如,如果要确保所有用户可见字符串都经过QObject::tr() 处理,这将非常有用。
如果该字符串在词法上小于参数字符串s ,则返回true
。否则返回false
。
另请参阅 QT_NO_CAST_FROM_ASCII 。
bool QStringRef::operator<=(const char *s) const
该函数重载了 operator<=()。
s const char 指针使用 fromUtf8() 函数转换为QStringRef 。
您可以在编译应用程序时通过定义QT_NO_CAST_FROM_ASCII
来禁用该操作符。例如,如果您想确保所有用户可见字符串都经过QObject::tr() 处理,这将非常有用。
如果该字符串在词法上小于或等于参数字符串s ,则返回true
。否则返回false
。
另请参阅 QT_NO_CAST_FROM_ASCII 。
QStringRef &QStringRef::operator=(const QString *string)
为给定的string 构建字符串引用,并将其赋值给该字符串引用,返回结果。
[noexcept default]
QStringRef &QStringRef::operator=(const QStringRef &other)
将other 字符串引用赋值给此字符串引用,并返回结果。
bool QStringRef::operator==(const char *s) const
该函数重载了 operator==()。
使用 fromUtf8() 函数将s 字节数组转换为QStringRef 。该函数会在发现第一个 NUL 字符或字节数组结束时停止转换。
在编译应用程序时,可以通过定义QT_NO_CAST_FROM_ASCII
来禁用该操作符。例如,如果您想确保所有用户可见字符串都通过QObject::tr() 转换,这将非常有用。
如果该字符串在词法上等于参数字符串s ,则返回true
。否则返回false
。
另请参阅 QT_NO_CAST_FROM_ASCII 。
bool QStringRef::operator>(const char *s) const
该函数重载了 operator>()。
s 常量字符指针使用 fromUtf8() 函数转换为QStringRef 。
您可以在编译应用程序时通过定义QT_NO_CAST_FROM_ASCII
来禁用该操作符。例如,如果要确保所有用户可见字符串都经过QObject::tr() 处理,这将非常有用。
如果该字符串在词法上大于参数字符串s ,则返回true
。否则返回false
。
另请参阅 QT_NO_CAST_FROM_ASCII 。
bool QStringRef::operator>=(const char *s) const
该函数重载了 operator>=()。
s const char 指针使用 fromUtf8() 函数转换为QStringRef 。
您可以在编译应用程序时通过定义QT_NO_CAST_FROM_ASCII
来禁用此操作符。例如,如果您想确保所有用户可见字符串都经过QObject::tr() 处理,这将非常有用。
如果该字符串在词法上大于或等于参数字符串s ,则返回true
。否则返回false
。
另请参阅 QT_NO_CAST_FROM_ASCII 。
QChar QStringRef::operator[](int position) const
返回字符串引用中给定索引position 处的字符。
position 必须是字符串引用中的有效索引位置(即 0 <=position <size() )。
另请参阅 at()。
相关非成员
[noexcept]
bool operator<(const QStringRef &s1, const QStringRef &s2)
如果字符串引用s1 在词法上小于字符串引用s2 ,则返回true
;否则返回false
。
另请参见 Comparing Strings 。
[noexcept]
bool operator<=(const QStringRef &s1, const QStringRef &s2)
如果字符串引用s1 在词法上小于或等于字符串引用s2 ,则返回true
;否则返回false
。
另请参见 Comparing Strings 。
[noexcept]
bool operator==(QLatin1String s1, const QStringRef &s2)
如果字符串s1 在词法上等于字符串引用s2 ,则返回true
;否则返回false
。
[noexcept]
bool operator==(const QString &s1, const QStringRef &s2)
如果字符串s1 在词法上等于字符串引用s2 ,则返回true
;否则返回false
。
[noexcept]
bool operator==(const QStringRef &s1, const QStringRef &s2)
如果字符串引用s1 在词法上等于字符串引用s2 ,则返回true
;否则返回false
。
[noexcept]
bool operator>(const QStringRef &s1, const QStringRef &s2)
如果字符串引用s1 在词法上大于字符串引用s2 ,则返回true
;否则返回false
。
另请参见 Comparing Strings 。
[noexcept]
bool operator>=(const QStringRef &s1, const QStringRef &s2)
如果字符串引用s1 在词法上大于或等于字符串引用s2 ,则返回true
;否则返回false
。
另请参见 Comparing Strings 。
© 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.