diff --git a/QtQStringFunEx/ExQStringFun.cpp b/QtQStringFunEx/ExQStringFun.cpp new file mode 100644 index 0000000..184a722 --- /dev/null +++ b/QtQStringFunEx/ExQStringFun.cpp @@ -0,0 +1,198 @@ +#include "ExQStringFun.h" +#include "ui_ExQStringFun.h" + +ExQStringFun::ExQStringFun(QWidget* parent) : + QWidget(parent), + ui(new Ui::ExQStringFun) +{ + ui->setupUi(this); +} + +ExQStringFun::~ExQStringFun() +{ + delete ui; +} + +//字符串相关 +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +void ExQStringFun::on_btnAppend_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str2 = ui->comboxStr2->currentText(); + str1.append(str2); + ui->editResult->setText(str1); +} + +void ExQStringFun::on_btnPrepend_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str2 = ui->comboxStr2->currentText(); + str1.prepend(str2); + ui->editResult->setText(str1); +} + +void ExQStringFun::on_btnToUpper_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString strRet = str1.toUpper(); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnToLower_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString strRet = str1.toLower(); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnLeft_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + int n = ui->spinLabSpin->value(); + QString strRet = str1.left(n); + ui->editResult->setText(strRet); + +} + +void ExQStringFun::on_btnRight_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + int n = ui->spinLabSpin->value(); + QString strRet = str1.right(n); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnSection_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str2 = ui->comboxStr2->currentText(); + int n = ui->spinLabSpin->value(); + + QString str3 = str1.section(str2, n, n+1); + ui->editResult->setText(str3); +} + +void ExQStringFun::on_btnSimplified_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString strRet = str1.simplified(); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnTrimmed_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString strRet = str1.trimmed(); + ui->editResult->setText(strRet); +} + +//数字相关 +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +void ExQStringFun::on_btnCount_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString strRet = QString("count():%1").arg(str1.count()); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnSize_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString strRet = QString("size():%1").arg(str1.size()); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnIndexOf_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str2 = ui->comboxStr2->currentText(); + + QString strRet = QString("IndexOf():%1").arg(str1.indexOf(str2)); + ui->editResult->setText(strRet); +} + + +void ExQStringFun::on_btnLastIndexOf_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str2 = ui->comboxStr2->currentText(); + + QString strRet = QString("lastIndexOf():%1").arg(str1.lastIndexOf(str2)); + ui->editResult->setText(strRet); +} + +//逻辑判断相关 +//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +void ExQStringFun::on_btnStartsWith_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str2 = ui->comboxStr2->currentText(); + QString str3 = ""; + + if (str1.startsWith(str2)) + str3 = "str1是以str2字符串开头"; + else + str3 = "str1不是以str2字符串开头"; + + QString strRet = QString("startsWith():%1").arg(str3); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnEndsWith_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str2 = ui->comboxStr2->currentText(); + QString str3 = ""; + + if (str1.endsWith(str2)) + str3 = "str1是以str2字符串结尾"; + else + str3 = "str1不是以str2字符串结尾"; + + QString strRet = QString("endsWith():%1").arg(str3); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnContains_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str2 = ui->comboxStr2->currentText(); + QString str3 = ""; + + if (str1.contains(str2)) + str3 = "str1字符中包含str2字符串"; + else + str3 = "str1字符中不包含str2字符串"; + + QString strRet = QString("contains():%1").arg(str3); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnIsNull_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str = "未赋值"; + + if(str1.isNull()) + str = "true"; + else + str = "false"; + + QString strRet = QString("str1.isNull() => %1").arg(str); + ui->editResult->setText(strRet); +} + +void ExQStringFun::on_btnIsEmpty_clicked() +{ + QString str1 = ui->comboxStr1->currentText(); + QString str = "未赋值"; + + if(str1.isNull()) + str = "true"; + else + str = "false"; + + QString strRet = QString("str1.isEmpty() => %1").arg(str); + ui->editResult->setText(strRet); +} diff --git a/QtQStringFunEx/ExQStringFun.h b/QtQStringFunEx/ExQStringFun.h new file mode 100644 index 0000000..e33499f --- /dev/null +++ b/QtQStringFunEx/ExQStringFun.h @@ -0,0 +1,60 @@ +#ifndef EXQSTRINGFUN_H +#define EXQSTRINGFUN_H + +#include + +namespace Ui { +class ExQStringFun; +} + +class ExQStringFun : public QWidget +{ + Q_OBJECT + +public: + explicit ExQStringFun(QWidget *parent = nullptr); + ~ExQStringFun(); + +private slots: + void on_btnAppend_clicked(); + + void on_btnPrepend_clicked(); + + void on_btnToUpper_clicked(); + + void on_btnToLower_clicked(); + + void on_btnLeft_clicked(); + + void on_btnRight_clicked(); + + void on_btnSection_clicked(); + + void on_btnSimplified_clicked(); + + void on_btnTrimmed_clicked(); + + void on_btnCount_clicked(); + + void on_btnSize_clicked(); + + void on_btnIndexOf_clicked(); + + + void on_btnLastIndexOf_clicked(); + + void on_btnStartsWith_clicked(); + + void on_btnEndsWith_clicked(); + + void on_btnContains_clicked(); + + void on_btnIsNull_clicked(); + + void on_btnIsEmpty_clicked(); + +private: + Ui::ExQStringFun *ui; +}; + +#endif // EXQSTRINGFUN_H diff --git a/QtQStringFunEx/ExQStringFun.ui b/QtQStringFunEx/ExQStringFun.ui new file mode 100644 index 0000000..92deb3d --- /dev/null +++ b/QtQStringFunEx/ExQStringFun.ui @@ -0,0 +1,686 @@ + + + ExQStringFun + + + + 0 + 0 + 675 + 865 + + + + ExQStringFun + + + + + + + + + + + 100 + 16777215 + + + + CheckBox + + + + + + + Qt::Horizontal + + + QSizePolicy::Preferred + + + + 60 + 20 + + + + + + + + + 16777215 + 16777215 + + + + left(int n)和right(int n)、section()函数使用,输入参数: + + + + + + + + 120 + 16777215 + + + + 3 + + + + + + + + + true + + + true + + + D:\programming\qt\QtExamples\QtQStringFunEx\main.cpp + + + + D:\programming\qt\QtExamples\QtQStringFunEx\main.cpp + + + + + D:\programming\qt\QtExamples\QtQStringFunEx + + + + + 投我以木李,报之以琼玖, 男,1998-07-07,汉族,湖北, 武汉 + + + + + csdn:https://blog.csdn.net/qq_33154343 + + + + + github:https://github.com/touwoyimuli + + + + + github.io:https://touwoyimuli.github.io/ + + + + + 或者自己自定义更多 + + + + + + + + + 0 + 0 + + + + + 50 + 20 + + + + + 75 + true + + + + str2: + + + + + + + true + + + true + + + \ + + + + \ + + + + + .cpp + + + + + : + + + + + + + + + + 或自定义更多尝试 + + + + + + + + + 50 + 20 + + + + + 75 + true + + + + str1: + + + + + + + + 75 + true + + + + 结果: + + + + + + + + + + ↓↓ + + + Qt::AlignCenter + + + + + + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 10 + + + + + + + + QString常用函数 + + + + + + + + + + + 0 + 20 + + + + + 75 + true + + + + 字符串相关: + + + + + + + perpend() + + + + + + + + 120 + 16777215 + + + + + 50 + false + + + + append() + + + + + + + 在字符串后面添加字符串 + + + + + + + right() + + + + + + + section + + + + + + + 从字符串中提取以“子字符串”作为分隔符,从start到end端的字符串 + + + + + + + trimmed + + + + + + + 不仅去掉字符串的所首尾空格,中间连续的空格也用一个空格替换 + + + + + + + 返回包含字符串中最右n个字符的子字符串。如果n大于或等于size()或小于零,则返回整个字符串。 + + + + + + + simplified + + + + + + + 去掉字符串首尾的空格 + + + + + + + 在字符串的前面添加字符串 + + + + + + + toUpper() + + + + + + + 将字符串的字母全部转换为大写字母 + + + + + + + 返回包含字符串中最左n个字符的子字符串。如果n大于或等于size()或小于零,则返回整个字符串。 + + + + + + + 将字符串的字母全部转换为大写字母 + + + + + + + toLower() + + + + + + + left() + + + + + + + + + Qt::Horizontal + + + + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 10 + + + + + + + + + + size + + + + + + + 同上 + + + + + + + 在字符串中查找子字符串str出现的位置。(Qt::CaseSensitivity cs 参数指定是否区分大小写) + + + + + + + indexOf + + + + + + + + 120 + 16777215 + + + + count + + + + + + + 在字符串中查找子字符串str最后出现的位置 + + + + + + + lastIndexOf + + + + + + + 返回字符串的字符个数。函数同size()、同length()。(字符串中若有汉字,一个汉字算一个字符) + + + + + + + + 0 + 20 + + + + + 16777215 + 30 + + + + + 75 + true + + + + 数字相关: + + + + + + + + + Qt::Horizontal + + + + + + + + + + 0 + 20 + + + + + 16777215 + 30 + + + + + 75 + true + + + + 逻辑判断: + + + + + + + 判断是否以某个字符串开头 + + + + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 10 + + + + + + + + endsWith + + + + + + + 判断是否以某个字符串结尾 + + + + + + + contains + + + + + + + 判断字符串是否为空。(若是只有“\0”,isNull返回false; 只有未赋值的字符串,isNull返回true) + + + + + + + 判断某个字符串中是否包含某个字符串 + + + + + + + isNull + + + + + + + isEmpty + + + + + + + + 120 + 16777215 + + + + startsWith + + + + + + + 判断字符串是否为空.(若是只有“\0”,isEmpty返回true) + + + + + + + + 75 + true + + + + 注意:QString只要赋值,就在字符串的末尾自动加上“\0” + + + + + + + + + + + + + + + + diff --git a/QtQStringFunEx/QT_win_32x32.ico b/QtQStringFunEx/QT_win_32x32.ico new file mode 100644 index 0000000..0ee0568 Binary files /dev/null and b/QtQStringFunEx/QT_win_32x32.ico differ diff --git a/QtQStringFunEx/QtQStringFunEx.pro b/QtQStringFunEx/QtQStringFunEx.pro new file mode 100644 index 0000000..3d5b7ff --- /dev/null +++ b/QtQStringFunEx/QtQStringFunEx.pro @@ -0,0 +1,42 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-08-18T14:08:16 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtQStringFunEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExQStringFun.cpp + +HEADERS += \ + ExQStringFun.h + +FORMS += \ + ExQStringFun.ui + +RC_ICONS += QT_win_32x32.ico + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/QtQStringFunEx/main.cpp b/QtQStringFunEx/main.cpp new file mode 100644 index 0000000..b235fd9 --- /dev/null +++ b/QtQStringFunEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExQStringFun.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExQStringFun w; + w.show(); + + return a.exec(); +}