QtQStringFunEx

update QtQStringFunEx
This commit is contained in:
touwoyimuli@gmail.com 2019-08-18 20:13:28 +08:00
parent 922ba02c7c
commit 4a280d42f6
6 changed files with 997 additions and 0 deletions

View File

@ -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);
}

View File

@ -0,0 +1,60 @@
#ifndef EXQSTRINGFUN_H
#define EXQSTRINGFUN_H
#include <QWidget>
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

View File

@ -0,0 +1,686 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExQStringFun</class>
<widget class="QWidget" name="ExQStringFun">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>675</width>
<height>865</height>
</rect>
</property>
<property name="windowTitle">
<string>ExQStringFun</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="0" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QCheckBox" name="chekbox">
<property name="maximumSize">
<size>
<width>100</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>CheckBox</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>60</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="labLabSpin">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>left(int n)和right(int n)、section()函数使用,输入参数:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinLabSpin">
<property name="maximumSize">
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
<property name="value">
<number>3</number>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboxStr1">
<property name="enabled">
<bool>true</bool>
</property>
<property name="editable">
<bool>true</bool>
</property>
<property name="currentText">
<string>D:\programming\qt\QtExamples\QtQStringFunEx\main.cpp</string>
</property>
<item>
<property name="text">
<string>D:\programming\qt\QtExamples\QtQStringFunEx\main.cpp</string>
</property>
</item>
<item>
<property name="text">
<string>D:\programming\qt\QtExamples\QtQStringFunEx</string>
</property>
</item>
<item>
<property name="text">
<string>投我以木李,报之以琼玖, 男1998-07-07汉族湖北, 武汉</string>
</property>
</item>
<item>
<property name="text">
<string>csdnhttps://blog.csdn.net/qq_33154343</string>
</property>
</item>
<item>
<property name="text">
<string>githubhttps://github.com/touwoyimuli</string>
</property>
</item>
<item>
<property name="text">
<string>github.iohttps://touwoyimuli.github.io/</string>
</property>
</item>
<item>
<property name="text">
<string>或者自己自定义更多</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labStr2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>50</width>
<height>20</height>
</size>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>str2:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboxStr2">
<property name="enabled">
<bool>true</bool>
</property>
<property name="editable">
<bool>true</bool>
</property>
<property name="currentText">
<string>\</string>
</property>
<item>
<property name="text">
<string>\</string>
</property>
</item>
<item>
<property name="text">
<string>.cpp</string>
</property>
</item>
<item>
<property name="text">
<string>:</string>
</property>
</item>
<item>
<property name="text">
<string></string>
</property>
</item>
<item>
<property name="text">
<string>或自定义更多尝试</string>
</property>
</item>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="labStr1">
<property name="maximumSize">
<size>
<width>50</width>
<height>20</height>
</size>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>str1:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labResult">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>结果:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="editResult"/>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_2">
<property name="text">
<string>↓↓</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>QString常用函数</string>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="labelQString">
<property name="minimumSize">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>字符串相关:</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="btnPrepend">
<property name="text">
<string>perpend()</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="btnAppend">
<property name="maximumSize">
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
<property name="font">
<font>
<weight>50</weight>
<bold>false</bold>
</font>
</property>
<property name="text">
<string>append()</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_11">
<property name="text">
<string>在字符串后面添加字符串</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QPushButton" name="btnRight">
<property name="text">
<string>right()</string>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QPushButton" name="btnSection">
<property name="text">
<string>section</string>
</property>
</widget>
</item>
<item row="8" column="1">
<widget class="QLabel" name="label_12">
<property name="text">
<string>从字符串中提取以“子字符串”作为分隔符从start到end端的字符串</string>
</property>
</widget>
</item>
<item row="10" column="0">
<widget class="QPushButton" name="btnTrimmed">
<property name="text">
<string>trimmed</string>
</property>
</widget>
</item>
<item row="9" column="1">
<widget class="QLabel" name="label_8">
<property name="text">
<string>不仅去掉字符串的所首尾空格,中间连续的空格也用一个空格替换</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLabel" name="label_10">
<property name="text">
<string>返回包含字符串中最右n个字符的子字符串。如果n大于或等于size()或小于零,则返回整个字符串。</string>
</property>
</widget>
</item>
<item row="9" column="0">
<widget class="QPushButton" name="btnSimplified">
<property name="text">
<string>simplified</string>
</property>
</widget>
</item>
<item row="10" column="1">
<widget class="QLabel" name="label_9">
<property name="text">
<string>去掉字符串首尾的空格</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="label_4">
<property name="text">
<string>在字符串的前面添加字符串</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QPushButton" name="btnToUpper">
<property name="text">
<string>toUpper()</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="label_5">
<property name="text">
<string>将字符串的字母全部转换为大写字母</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLabel" name="label_7">
<property name="text">
<string>返回包含字符串中最左n个字符的子字符串。如果n大于或等于size()或小于零,则返回整个字符串。</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="label_6">
<property name="text">
<string>将字符串的字母全部转换为大写字母</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QPushButton" name="btnToLower">
<property name="text">
<string>toLower()</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QPushButton" name="btnLeft">
<property name="text">
<string>left()</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="4" column="0">
<widget class="QPushButton" name="btnSize">
<property name="text">
<string>size</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="label_17">
<property name="text">
<string>同上</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="label_18">
<property name="text">
<string>在字符串中查找子字符串str出现的位置。Qt::CaseSensitivity cs 参数指定是否区分大小写)</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QPushButton" name="btnIndexOf">
<property name="text">
<string>indexOf</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="btnCount">
<property name="maximumSize">
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>count</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLabel" name="label_20">
<property name="text">
<string>在字符串中查找子字符串str最后出现的位置</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QPushButton" name="btnLastIndexOf">
<property name="text">
<string>lastIndexOf</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="label_21">
<property name="text">
<string>返回字符串的字符个数。函数同size()、同length()。(字符串中若有汉字,一个汉字算一个字符)</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="labelNumber">
<property name="minimumSize">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>30</height>
</size>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>数字相关:</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QGridLayout" name="gridLayout_4">
<item row="1" column="0" colspan="2">
<widget class="QLabel" name="labelBool">
<property name="minimumSize">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>30</height>
</size>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>逻辑判断:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_31">
<property name="text">
<string>判断是否以某个字符串开头</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>10</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="btnEndsWith">
<property name="text">
<string>endsWith</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="label_27">
<property name="text">
<string>判断是否以某个字符串结尾</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QPushButton" name="btnContains">
<property name="text">
<string>contains</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="label_30">
<property name="text">
<string>判断字符串是否为空。(若是只有“\0”isNull返回false 只有未赋值的字符串isNull返回true</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="label_28">
<property name="text">
<string>判断某个字符串中是否包含某个字符串</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QPushButton" name="btnIsNull">
<property name="text">
<string>isNull</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QPushButton" name="btnIsEmpty">
<property name="text">
<string>isEmpty</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="btnStartsWith">
<property name="maximumSize">
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>startsWith</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLabel" name="label_29">
<property name="text">
<string>判断字符串是否为空.(若是只有“\0”isEmpty返回true</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLabel" name="label">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>注意QString只要赋值就在字符串的末尾自动加上“\0”</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -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

11
QtQStringFunEx/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "ExQStringFun.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ExQStringFun w;
w.show();
return a.exec();
}