QT5笔记:13. QString 的常用功能

  • QString采用Unicode码,所以任何一个字符不管中英文,在size或者count时都算作一个字符,不会有宽字符区分

  • 常用的方法

    • append
    • prepend
    • toUpper
    • toLower
    • left
    • right
    • section
    • simplified
    • trimmed
    • count
    • size
    • indexOf
    • lastIndexOf
    • endsWith
    • startsWith
    • contains
    • isNull
    • isEmpty
    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::on_btnAppend_clicked()
    {
        QString str1 ,str2;
        str1 = ui->comboBoxStr1->currentText();//G:\Qt5Book\QT5.9Study
        str2 = ui->comboBoxStr2->currentText();//一个反斜杠
        str1 = str1.append(str2);
        ui->editResult->setText(str1);
    }
    
    void Widget::on_btnPrepend_clicked()
    {
        QString str1 ,str2;
        str1 = ui->comboBoxStr1->currentText();
        str2 = ui->comboBoxStr2->currentText();
        str1 = str1.prepend(str2);
        ui->editResult->setText(str1);
    }
    
    void Widget::on_btnUpper_clicked()
    {
        QString str1 = ui->comboBoxStr1->currentText();
        str1 = str1.toUpper();
        ui->editResult->setText(str1);
    }
    
    void Widget::on_btnLower_clicked()
    {
        QString str1 = ui->comboBoxStr1->currentText();
        str1 = str1.toLower();
        ui->editResult->setText(str1);
    }
    
    void Widget::on_btnLeft_clicked()
    {
        QString str1 = ui->comboBoxStr1->currentText();
        int number = ui->spinBoxResult->value();
        str1 = str1.left(number);
        ui->editResult->setText(str1);
    }
    
    void Widget::on_btnRight_clicked()
    {
        QString str1 = ui->comboBoxStr1->currentText();
        int number = ui->spinBoxResult->value();
        str1 = str1.right(number);
        ui->editResult->setText(str1);
    }
    
    /**
     * section
     * @brief Widget::on_btnSection_clicked
     * @note sep表示用来切割的字符,start表示开始切割的位置,end表示切割的结束位置,flag参数可以用来影响函数的行为的某些方面,例如是否区分大小写,是否跳过空字段和如何处理前导和尾随分隔符。
     * @result 结果返回开始切割那个字符到结束切割的那个字符之后的那个字符串。
     * @note 当start 或者 end的参数为负数时,表示起始位置为右边。
     * @note 相当于先split()进行切割,拿到各个片段(都是数组),然后取出index为start到end的数组内容,然后组装成一个字符串
     */
    void Widget::on_btnSection_clicked()
    {
        QString str1 ,str2;
        str1 = ui->comboBoxStr1->currentText();
        str2 = ui->comboBoxStr2->currentText();
        int number = ui->spinBoxResult->value();
        str1 = str1.section(str2.data()[0],number,number +1);
        ui->editResult->setText(str1);
    }
    
    /**
     * @brief Widget::on_btnSimplified_clicked
     * @note 将字符串的开头和结尾的空白字符(\t、\r、\n、\f、空格)去掉,然后字符串中间的若有多个空格相连,则只保留一个
     */
    void Widget::on_btnSimplified_clicked()
    {
        QString str1 = ui->comboBoxStr1->currentText();
        str1 = str1.simplified();
        ui->editResult->setText(str1);
    }
    
    /**
     * @brief Widget::on_btnTirm_clicked
     * @note 将字符串开头和结尾的空白字符(\t、\r、\n、\f、空格)去掉
     */
    void Widget::on_btnTirm_clicked()
    {
        QString str1 = ui->comboBoxStr1->currentText();
        str1 = str1.trimmed();
        ui->editResult->setText(str1);
    }
    
    /**
     * @brief Widget::on_btnCount_clicked
     * @note Returns the number of (potentially overlapping) occurrences of the string str in this string.
     */
    void Widget::on_btnCount_clicked()
    {
        QString str1 = ui->comboBoxStr1->currentText();
        int count = str1.count();
        ui->editResult->setText(str1.setNum(count));
    }
    
    /**
     * @brief Widget::on_btnSize_clicked
     * @note string的大小:Returns the number of characters in this string.
     */
    void Widget::on_btnSize_clicked()
    {
        QString str1 = ui->comboBoxStr1->currentText();
        int size = str1.size();
        ui->editResult->setText(str1.setNum(size));
    }
    
    /**
     * @brief Widget::on_binIndexOf_clicked
     * @note 第一次出现的index
     */
    void Widget::on_binIndexOf_clicked()
    {
        QString str1 ,str2;
        str1 = ui->comboBoxStr1->currentText();
        str2 = ui->comboBoxStr2->currentText();
        int index = str1.indexOf(str2);
        str1 = str1.setNum(index);
        ui->editResult->setText(str1);
    }
    
    /**
     * @brief Widget::on_btnLastIndex_clicked
     * @note 最后一次出现的index
     */
    void Widget::on_btnLastIndex_clicked()
    {
        QString str1 ,str2;
        str1 = ui->comboBoxStr1->currentText();
        str2 = ui->comboBoxStr2->currentText();
        int index = str1.lastIndexOf(str2);
        str1 = str1.setNum(index);
        ui->editResult->setText(str1);
    }
    
    /**
     * @brief Widget::on_btnEndWith_clicked
     * @note 是否是以str2结尾
     */
    void Widget::on_btnEndWith_clicked()
    {
        QString str1 ,str2;
        str1 = ui->comboBoxStr1->currentText();
        str2 = ui->comboBoxStr2->currentText();
        bool result = str1.endsWith(str2);
        ui->checkBoxResult->setText(qobject_cast<QPushButton *>(sender())->text());
        ui->checkBoxResult->setChecked(result);
    }
    
    /**
     * @brief Widget::on_btnStartsWith_clicked
     * @note 是否以str2开始
     */
    void Widget::on_btnStartsWith_clicked()
    {
        QString str1 ,str2;
        str1 = ui->comboBoxStr1->currentText();
        str2 = ui->comboBoxStr2->currentText();
        bool result = str1.startsWith(str2);
        ui->checkBoxResult->setText(qobject_cast<QPushButton *>(sender())->text());
        ui->checkBoxResult->setChecked(result);
    }
    
    /**
     * @brief Widget::on_btnContains_clicked
     * @note 是否存在str2
     */
    void Widget::on_btnContains_clicked()
    {
        QString str1 ,str2;
        str1 = ui->comboBoxStr1->currentText();
        str2 = ui->comboBoxStr2->currentText();
        bool result = str1.contains(str2);
        ui->checkBoxResult->setText(qobject_cast<QPushButton *>(sender())->text());
        ui->checkBoxResult->setChecked(result);
    }
    
    /**
     * @brief Widget::on_btnIsNull_clicked
     * 是否是null,区别于空字符串和空白字符串
     */
    void Widget::on_btnIsNull_clicked()
    {
        QString str1 = ui->comboBoxStr1->currentText();
        bool result = str1.isNull();
        ui->checkBoxResult->setText(qobject_cast<QPushButton *>(sender())->text());
        ui->checkBoxResult->setChecked(result);
    }
    
    /**
     * @brief Widget::on_btnIsEmpty_clicked
     * 是否是空字符串
     */
    void Widget::on_btnIsEmpty_clicked()
    {
        QString str1 ,str2;
        str1 = ui->comboBoxStr1->currentText();
        bool result = str1.isEmpty();
        ui->checkBoxResult->setText(qobject_cast<QPushButton *>(sender())->text());
        ui->checkBoxResult->setChecked(result);
    }
    
    

    界面:
    QString的常用功能

posted @ 2023-03-15 17:31  echo_lovely  阅读(48)  评论(0编辑  收藏  举报