📄 qstring.cpp
字号:
This is the same as replace(\a ch, "", \a cs).*/QString &QString::remove(QChar ch, Qt::CaseSensitivity cs){ int i = 0; if (cs == Qt::CaseSensitive) { while (i < d->size) if (*((const QChar*)d->data + i) == ch) remove(i, 1); else i++; } else { ch = ::lower(ch); while (i < d->size) if (::lower(*((const QChar*)d->data + i)) == ch) remove(i, 1); else i++; } return *this;}/*! \fn QString &QString::remove(const QRegExp &rx) \overload Removes every occurrence of the regular expression \a rx in the string, and returns a reference to the string. For example: \code QString str = "Telephone"; str.remove(QRegExp("[aeiou].")); // str == "The" \endcode \sa indexOf(), lastIndexOf(), replace()*//*! Replaces \a len characters from index position \a pos with the string \a after, and returns a reference to this string. Example: \code QString x = "Say yes!"; QString y = "no"; x.replace(4, 3, y); // x == "Say no!" \endcode \sa insert(), remove()*/QString &QString::replace(int pos, int len, const QString &after){ QString copy = after; remove(pos, len); return insert(pos, copy.constData(), copy.d->size);}/*! \overload Replaces \a len characters from index position \a pos with the first \a size characters of the QChar array \a unicode.*/QString &QString::replace(int pos, int len, const QChar *unicode, int size){ remove(pos, len); return insert(pos, unicode, size);}/*! \overload Replaces \a len characters from index position \a pos with the character \a after.*/QString &QString::replace(int pos, int len, QChar after){ remove(pos, len); return insert(pos, after);}/*! \overload Replaces every occurrence of the string \a before with the string \a after. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive. Example: \code QString str = "colour behaviour flavour neighbour"; str.replace(QString("ou"), QString("o")); // str == "color behavior flavor neighbor" \endcode*/QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs){ if (d->size == 0) { if (before.d->size) return *this; } else { if (cs == Qt::CaseSensitive && before == after) return *this; } if (d->ref != 1) realloc(d->size); QStringMatcher matcher(before, cs); int index = 0; const int bl = before.d->size; const int al = after.d->size; if (bl == al) { if (bl) { const QChar *auc = (const QChar*) after.d->data; while ((index = matcher.indexIn(*this, index)) != -1) { // we need memmove(), not memcpy(), in the rare case where // this == &after, before == after, and cs is Qt::CaseInsensitive memmove(d->data + index, auc, al * sizeof(QChar)); index += bl; } } } else if (al < bl) { const QChar *auc = after.unicode(); uint to = 0; uint movestart = 0; uint num = 0; while ((index = matcher.indexIn(*this, index)) != -1) { if (num) { int msize = index - movestart; if (msize > 0) { memmove(d->data + to, d->data + movestart, msize * sizeof(QChar)); to += msize; } } else { to = index; } if (al) { memcpy(d->data+to, auc, al*sizeof(QChar)); to += al; } index += bl; movestart = index; num++; } if (num) { int msize = d->size - movestart; if (msize > 0) memmove(d->data + to, d->data + movestart, msize * sizeof(QChar)); resize(d->size - num*(bl-al)); } } else { const QString copy = after; // the most complex case. We don't want to lose performance by doing repeated // copies and reallocs of the string. while (index != -1) { uint indices[4096]; uint pos = 0; while (pos < 4095) { index = matcher.indexIn(*this, index); if (index == -1) break; indices[pos++] = index; index += bl; // avoid infinite loop if (!bl) index++; } if (!pos) break; // we have a table of replacement positions, use them for fast replacing int adjust = pos*(al-bl); // index has to be adjusted in case we get back into the loop above. if (index != -1) index += adjust; int newLen = d->size + adjust; int moveend = d->size; if (newLen > d->size) resize(newLen); while (pos) { pos--; int movestart = indices[pos] + bl; int insertstart = indices[pos] + pos*(al-bl); int moveto = insertstart + al; memmove(d->data + moveto, d->data + movestart, (moveend - movestart)*sizeof(QChar)); memcpy(d->data + insertstart, copy.unicode(), al*sizeof(QChar)); moveend = movestart-bl; } } } return *this;}/*! \overload Replaces every occurrence of the character \a ch in the string with \a after. Returns a reference to the string. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive.*/QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs){ return replace(QString(ch), after, cs);}/*! \overload Replaces every occurrence of the character \a before with the character \a after. Returns a reference to the string. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive.*/QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs){ if (d->size) { QChar *i = data(); QChar *e = i + d->size; if (cs == Qt::CaseSensitive) { for (; i != e; ++i) if (*i == before) * i = after; } else { before = ::lower(before); for (; i != e; ++i) if (::lower(*i) == before) * i = after; } } return *this;}/*! Returns true if string \a other is equal to this string; otherwise returns false. The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with localeAwareCompare().*/bool QString::operator==(const QString &other) const{ return (size() == other.size()) && (memcmp((char*)unicode(),(char*)other.unicode(), size()*sizeof(QChar))==0);}/*! \overload*/bool QString::operator==(const QLatin1String &other) const{ const ushort *uc = d->data; const ushort *e = uc + d->size; const uchar *c = (uchar *)other.latin1(); if (!c) return isEmpty(); while (*c) { if (uc == e || *uc != *c) return false; ++uc; ++c; } return (uc == e);}/*! \fn bool QString::operator==(const QByteArray &other) const \overload \a other is converted to a QString using fromAscii(). You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn bool QString::operator==(const char *other) const \overload \a other is converted to a QString using fromAscii(). You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! Returns true if this string is lexically less than string \a other; otherwise returns false. The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with localeAwareCompare().*/bool QString::operator<(const QString &other) const{ return ucstrcmp(*this, other) < 0;}/*! \overload*/bool QString::operator<(const QLatin1String &other) const{ const ushort *uc = d->data; const ushort *e = uc + d->size; const uchar *c = (uchar *) other.latin1(); if (!c || *c == 0) return false; while (*c) { if (uc == e || *uc != *c) break; ++uc; ++c; } return (uc == e ? *c : *uc < *c);}/*! \fn bool QString::operator<(const QByteArray &other) const \overload \a other is converted to a QString using fromAscii(). You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn bool QString::operator<(const char *other) const \overload \a other is converted to a QString using fromAscii(). You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn bool QString::operator<=(const QString &other) const Returns true if this string is lexically less than or equal to string \a other; otherwise returns false. The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with localeAwareCompare().*//*! \fn bool QString::operator<=(const QLatin1String &other) const \overload*//*! \fn bool QString::operator<=(const QByteArray &other) const \overload \a other is converted to a QString using fromAscii(). You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn bool QString::operator<=(const char *other) const \overload \a other is converted to a QString using fromAscii(). You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn bool QString::operator>(const QString &other) const Returns true if this string is lexically greater than string \a other; otherwise returns false. The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with localeAwareCompare().*//*! \overload*/bool QString::operator>(const QLatin1String &other) const{ const ushort *uc = d->data;; const ushort *e = uc + d->size; const uchar *c = (uchar *) other.latin1(); if (!c || *c == '\0') return !isEmpty(); while (*c) { if (uc == e || *uc != *c) break; ++uc; ++c; } return (uc == e ? false : *uc > *c);}/*! \fn bool QString::operator>(const QByteArray &other) const \overload \a other is converted to a QString using fromAscii(). You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn bool QString::operator>(const char *other) const \overload \a other is converted to a QString using fromAscii().
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -