QT6 源(61)篇四:阅读与注释 QString 这个类,先给出其应用举例,insert()、replace()、remove()、单列出 QString 与 c 语言字符串交互的函数从构造到运算符

(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)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值