(6)
++
++
++
++接着学习 replace() :
//Replaces len characters beginning at index i with the character after and
//returns a reference to this string.
QString & replace(qsizetype i, qsizetype len, QChar after);
QString & replace(qsizetype i, qsizetype len, const QChar * s, qsizetype slen);
//Replaces len characters beginning at index i with the
//first slen characters of the QChar array s and returns a
//reference to this string.
QString & replace(qsizetype i, qsizetype len, const QString & after);
//Replaces len characters beginning at index i with the string after and
//returns a reference to this string.
//Note: If the specified position i is within the string,
//but i + len goes outside the strings range,
//then len will be adjusted to stop at the end of the string.
//Replaces every occurrence of the character before with the
//character after and returns a reference to this string.
//If cs is Qt::CaseSensitive (default), //替换原字符串里所有的 before字符
//the search is case sensitive; otherwise the search is case insensitive.
QString & replace( QChar before, QChar after,
Qt::CaseSensitivity cs = Qt::CaseSensitive);
QString & replace( QChar c , const QString & after, //多处替换
Qt::CaseSensitivity cs = Qt::CaseSensitive);
//Replaces every occurrence of the character c in the string with after and
//returns a reference to this string.
QString & replace(const QChar * before, qsizetype blen , //多处替换
const QChar * after , qsizetype alen ,
Qt::CaseSensitivity cs = Qt::CaseSensitive);
//Replaces each occurrence in this string of the first blen characters of
//before with the first alen characters of after and returns a
//reference to this string.
QString & replace(const QString & before, const QString & after,
Qt::CaseSensitivity cs = Qt::CaseSensitive); //多处替换
//Replaces every occurrence of the string before with the
//string after and returns a reference to this string.
++ 给出举例:
++ 接着学习另一个重要的函数 remove():
template <typename Predicate>
QString &removeIf(Predicate pred)
{
QtPrivate::sequential_erase_if(*this, pred);
return *this;
}
//Removes len characters from the string, starting at the given position i,
//and returns a reference to the string.
//If the specified position i is within the string, but i + len is beyond the
//end of the string, the string is truncated at the specified position.
//Element removal will preserve the string's capacity and not reduce the
//amount of allocated memory.
//To shed extra capacity and free as much memory as possible,
//call squeeze() after the last change to the string's size.
QString & remove( qsizetype i, qsizetype len);
//Removes every occurrence of the character c in this string,
//and returns a reference to this string.
//If cs is Qt::CaseSensitive (default), the search is case sensitive;
//otherwise the search is case insensitive.
QString & remove( QChar c,
Qt::CaseSensitivity cs = Qt::CaseSensitive);
QString & remove(const QString & s,
Qt::CaseSensitivity cs = Qt::CaseSensitive);
//Removes every occurrence of the given s string in this string,
//and returns a reference to this string.
QString & remove( QLatin1String s,
Qt::CaseSensitivity cs = Qt::CaseSensitive);
++给出举例:
(7) 单列出 QString 与 c 语言字符串交互的函数从构造到运算符 :
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
QT_ASCII_CAST_WARN inline QString(const char *ch)
: QString(fromUtf8(ch)) {}
QT_ASCII_CAST_WARN inline QString(const QByteArray &a)
: QString(fromUtf8(a)) {}
QT_ASCII_CAST_WARN inline QString &operator=(const char *ch)
{ return (*this = fromUtf8(ch)); }
QT_ASCII_CAST_WARN inline QString &operator=(const QByteArray &a)
{ return (*this = fromUtf8(a)); }
// these are needed, so it compiles with STL support enabled
QT_ASCII_CAST_WARN inline QString &prepend(const char *s)
{ return prepend(QString::fromUtf8(s)); }
QT_ASCII_CAST_WARN inline QString &prepend(const QByteArray &s)
{ return prepend(QString::fromUtf8(s)); }
QT_ASCII_CAST_WARN inline QString &append(const char *s)
{ return append(QString::fromUtf8(s)); }
QT_ASCII_CAST_WARN inline QString &append(const QByteArray &s)
{ return append(QString::fromUtf8(s)); }
QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const char *s)
{ return insert(i, QString::fromUtf8(s)); }
QT_ASCII_CAST_WARN inline QString &insert(qsizetype i, const QByteArray &s)
{ return insert(i, QString::fromUtf8(s)); }
QT_ASCII_CAST_WARN inline QString &operator+=(const char *s)
{ return append(QString::fromUtf8(s)); }
QT_ASCII_CAST_WARN inline QString &operator+=(const QByteArray &s)
{ return append(QString::fromUtf8(s)); }
QT_ASCII_CAST_WARN inline bool operator==(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator<(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator<=(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator>(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator>=(const char *s) const;
QT_ASCII_CAST_WARN inline bool operator==(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator!=(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator<(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator>(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const;
QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const;
QT_ASCII_CAST_WARN friend bool operator==(const char *s1, const QString &s2)
{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; }
QT_ASCII_CAST_WARN friend bool operator!=(const char *s1, const QString &s2)
{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; }
QT_ASCII_CAST_WARN friend bool operator< (const char *s1, const QString &s2)
{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; }
QT_ASCII_CAST_WARN friend bool operator> (const char *s1, const QString &s2)
{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; }
QT_ASCII_CAST_WARN friend bool operator<=(const char *s1, const QString &s2)
{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; }
QT_ASCII_CAST_WARN friend bool operator>=(const char *s1, const QString &s2)
{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; }
#endif
(8)
(9)
谢谢