当前位置: 首页 > article >正文

【Qt | Qstring】Qstring详细介绍(字符串的添加、删除、修改、查询、截断、删除空白字符等)

😁博客主页😁:🚀https://blog.csdn.net/wkd_007🚀
🤑博客内容🤑:🍭嵌入式开发、Linux、C语言、C++、数据结构、音视频🍭
⏰发布时间⏰: 2024-09-24 09:10:24

本文未经允许,不得转发!!!

目录

  • 🎄一、概述
  • 🎄二、增加、组合、拼接 字符串
    • ✨2.1 构造字符串
    • ✨2.2 给 QString 对象 赋值
    • ✨2.3 添加字符串到 QString 对象尾部
    • ✨2.4 添加字符串到 QString 对象开头
    • ✨2.5 添加字符串到 QString 对象指定位置
    • ✨2.6 其他添加、拼接字符串的方式
  • 🎄三、QString对象的 清空、 删除
    • ✨3.1 清空字符串
    • ✨3.2 从字符串末尾删除字符
    • ✨3.3 删除指定字符串、指定位置的子字符串
    • ✨3.4 删除空白字符
    • ✨3.5 截断字符串
  • 🎄四、修改 字符串
    • ✨4.1 修改字符串大小
    • ✨4.2 替换掉字符串的字符
    • ✨4.3 交换字符串
    • ✨4.3 字符串格式转换
  • 🎄五、从字符串 查询
    • ✨5.1 查询字符串的字符个数
    • ✨5.2 查询字符串的一个字符
    • ✨5.3 查询字符串是否为空、是否为大小写
    • ✨5.4 查询是否包含指定的 子字符串
    • ✨5.5 查询 子字符串 的位置
    • ✨5.6 查询 子字符串 的出现的个数
    • ✨5.7 获取一个 子字符串
    • ✨5.8 将字符串分割成多个 子字符串
  • 🎄六、其他


在这里插入图片描述
在这里插入图片描述

🎄一、概述

标准 C++ 提供了两种字符串: 一种是 C 语言风格的以 \0 字符结尾的字符数组;另 一种是字符串类 String 。 而 Qt 字符串类 QString 的功能更强大。

QString 类保存 16 位 Unicode 值,提供了丰富的操作、查询和转换等函数。该类还进行了使用隐式共享 (implicit sharing) 、高效的内存分配策略等多方面的优化。

QString存储一个16位QChar字符串,其中每个QChar对应一个UTF-16编码单元。(编码值大于65535的Unicode字符使用代理对存储,即两个连续的QChar。)

Unicode是一个国际标准,支持目前使用的大多数书写系统。它是US-ASCII (ANSI X3.4-1986)和Latin-1 (ISO 8859-1)的超集,并且所有US-ASCII/Latin-1字符都在相同的代码位置上可用。

实现时,QString使用隐式共享(implicit sharing,也就是写时复制)来减少内存使用并避免不必要的数据复制。这也有助于减少存储16位字符而不是8位字符的固有开销。

除了QString, Qt还提供了QByteArray类来存储原始字节和传统的8位’\0’结尾的字符串。在大多数情况下,QString是您想要使用的类。 它在整个Qt API中使用,并且Unicode支持确保如果您希望在某个时候扩展应用程序的市场,您的应用程序将易于翻译。适合使用QByteArray的两种主要情况是:当您需要存储原始二进制数据时,以及当内存比较紧张时(如在嵌入式系统中)。


在这里插入图片描述

🎄二、增加、组合、拼接 字符串

✨2.1 构造字符串

QString 的构造函数有下面几个:

QString()
QString(const QChar *unicode, int size = -1)
QString(QChar ch)
QString(int size, QChar ch)
QString(QLatin1String str)
QString(const QString &other)
QString(QString &&other)
QString(const char *str)
QString(const QByteArray &ba)

其中,最简单的,也最常用的是,直接用一个以\0结尾的char*字符串给QString对象初始化,如下:

// 1.1 QString(const char *str)
QString str_first("str_first");
qDebug() << str_first << "," << str_second; // "str_first" , "str_second"

下面是其他构造函数例子:

// 1.2 QString(const QChar *unicode, int size = -1)
static const QChar data[4] = { 65, 66, 67, 68 };    // ABCD 的UniCode码
QString str_data(data, 4);
qDebug() << str_data ;                      // "ABCD"

// 1.3 QString(QChar ch)
//     QString(int size, QChar ch)
QChar qchar_a = 'a';
QString str_qchar(qchar_a);
QString str_qchar2(2, qchar_a);
qDebug() << str_qchar << "," << str_qchar2; // "a" , "aa"

// 1.4 QString(QLatin1String str)
QString str_Latin1(QLatin1String("static"));
qDebug() << str_Latin1;                     // "static"

// 1.5 QString(const QString &other)
//     QString(QString &&other)
QString strString(str_first);
qDebug() << strString;                      // "str_first"

// 1.6 QString(const QByteArray &ba)
QString strByteArray(QByteArray("efgh"));
qDebug() << strByteArray;                   // "efgh"

其中,QLatin1String类提供了 US-ASCII/Latin-1 编码字符串字面值的薄包装。


✨2.2 给 QString 对象 赋值

如果实例化一个 QString 对象后,可以使用 = 运算符给该对象赋值,函数原型如下:

QString &operator=(const QString &other)
QString &operator=(QChar ch)
QString &operator=(QLatin1String str)
QString &operator=(QString &&other)
QString &operator=(const char *str)
QString &operator=(const QByteArray &ba)
QString &operator=(char ch)

🌰举例子:

QString str_second = str_first;
qDebug() << (str_second = QChar('A'));
qDebug() << (str_second = QLatin1String("str_second1"));
qDebug() << (str_second = "str_second2");
qDebug() << (str_second = QByteArray("str_second3"));
qDebug() << (str_second = 'B');

🎯运行结果:
在这里插入图片描述


✨2.3 添加字符串到 QString 对象尾部

添加字符串到 QString 对象尾部的相关函数原型如下:

// 添加到字符串尾部
QString &append(const QString &str)
QString &append(const QChar *str, int len)
QString &append(QChar ch)
QString &append(const QStringRef &reference)
QString &append(QLatin1String str)
QString &append(const char *str)
QString &append(const QByteArray &ba)

// 提供此函数是为了兼容STL,将给定的其他字符串附加到该字符串的末尾。它相当于append(other)。
void push_back(const QString &other)
void push_back(QChar ch)

// += 运算符(公开成员函数)
QString &operator+=(const QString &other)
QString &operator+=(QChar ch)
QString &operator+=(const QStringRef &str)
QString &operator+=(QLatin1String str)
QString &operator+=(const char *str)
QString &operator+=(const QByteArray &ba)
QString &operator+=(char ch)

// + 运算符(友元函数)
const QString operator+(const QString &s1, const QString &s2)
const QString operator+(const QString &s1, const char *s2)
const QString operator+(const QString &s, char ch)

上面这几种最常用的是+运算符,使用比较方便,而且可以连续加,如:a+b+c。

🌰举例子:

// 2.3 添加字符串到 QString 对象尾部
QString strAdd("strAdd");
qDebug() << strAdd.append(QString("1"));
qDebug() << strAdd.append(QChar('2'));
qDebug() << strAdd.append("3");
qDebug() << strAdd.append(QByteArray("4"));
qDebug(" ");

strAdd.push_back(QString("5"));
qDebug() << strAdd;
strAdd.push_back(QString('6'));
qDebug() << strAdd;
qDebug(" ");

qDebug() << (strAdd += QString("7"));
qDebug() << (strAdd += QChar('8') );
qDebug() << (strAdd += "9" );
qDebug() << (strAdd += 'a' );
qDebug() << (strAdd += QByteArray("b") );
qDebug(" ");

qDebug() << (strAdd + QString("c"));
qDebug() << (strAdd + "d");
qDebug() << (strAdd + 'e');
qDebug(" ");

🎯运行结果:
在这里插入图片描述


✨2.4 添加字符串到 QString 对象开头

// 将字符串str添加到该字符串的开头,并返回对该字符串的引用。
QString &prepend(const QString &str)
QString &prepend(const QChar *str, int len)
QString &prepend(QChar ch)
QString &prepend(const QStringRef &str)
QString &prepend(QLatin1String str)
QString &prepend(const char *str)
QString &prepend(const QByteArray &ba)

// 提供此函数是为了兼容STL,将给定的其他字符串附加到该字符串的末尾。它相当于prepend(other)。
void push_front(const QString &other)
void push_front(QChar ch)

// + 运算符(友元函数)
const QString operator+(const char *s1, const QString &s2)
const QString operator+(char ch, const QString &s)

🌰举例子:

// 2.4 添加字符串到 QString 对象开头
QString strHead("strHead");
qDebug() << strHead.prepend(QString("1"));
qDebug() << strHead.prepend(QChar('2'));
qDebug() << strHead.prepend(QLatin1String("3"));
qDebug() << strHead.prepend("4");
qDebug() << strHead.prepend(QByteArray("5"));
qDebug(" ");

strHead.push_front(QString("6"));
qDebug() << strHead;
strHead.push_front(QChar('7'));
qDebug() << strHead;
qDebug(" ");

qDebug() << "8" + strHead;
qDebug() << '9' + strHead;
qDebug(" ");

🎯运行结果:
在这里插入图片描述


✨2.5 添加字符串到 QString 对象指定位置

// 插入到指定位置
QString &insert(int position, const QString &str)
QString &insert(int position, const QChar *unicode, int size)
QString &insert(int position, QChar ch)
QString &insert(int position, const QStringRef &str)
QString &insert(int position, QLatin1String str)
QString &insert(int position, const char *str)
QString &insert(int position, const QByteArray &str)

🌰举例子:

// 2.5 添加字符串到 QString 对象指定位置
QString strInsert("strInsert");
qDebug() << strInsert.insert(1,"1");
qDebug() << strInsert.insert(2,QString("2"));
qDebug() << strInsert.insert(3,QChar('3'));
qDebug() << strInsert.insert(4,QByteArray("4"));

🎯运行结果:
在这里插入图片描述


✨2.6 其他添加、拼接字符串的方式

添加字符串有好几种方式,最常用的是直接使用+运算符,下面给出添加字符串的函数原型:

//将字符串中的每个字符设置为字符ch。如果size与-1(默认值)不同,则事先将字符串大小调整为size。
QString &QString::fill(QChar ch, int size = -1)

// 将字符串设置为指定基数中n的打印值,并返回对该字符串的引用。
// 基数默认为10,取值范围必须在2到36之间。对于非10的基数,n被视为无符号整数。
QString &setNum(int n, int base = 10)
QString &setNum(ushort n, int base = 10)
QString &setNum(short n, int base = 10)
QString &setNum(uint n, int base = 10)
QString &setNum(long n, int base = 10)
QString &setNum(ulong n, int base = 10)
QString &setNum(qlonglong n, int base = 10)
QString &setNum(qulonglong n, int base = 10)
QString &setNum(float n, char format = 'g', int precision = 6)
QString &setNum(double n, char format = 'g', int precision = 6)

// 此函数支待的格式定义符和 C廿库中的函数 sprintf()定义的一样。
String &sprintf(const char *format, ...)

// 返回此字符串的副本,其中编号最低的位置标记替换为字符串a,即%1,%2,…, %99。
// fieldWidth指定参数a应占用的最小空间量。如果a所需的空间小于fieldWidth,则使用字符fillChar填充到fieldWidth。一个正的fieldWidth将产生右对齐的文本。负fieldWidth将生成左对齐的文本。
QString arg(const QString &a, int fieldWidth = 0, QChar fillChar = QLatin1Char(' ')) const
QString arg(qulonglong a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
QString arg(long a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
QString arg(ulong a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
QString arg(int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
QString arg(uint a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
QString arg(short a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
QString arg(ushort a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
QString arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char(' ')) const
QString arg(char a, int fieldWidth = 0, QChar fillChar = QLatin1Char(' ')) const
QString arg(QChar a, int fieldWidth = 0, QChar fillChar = QLatin1Char(' ')) const
QString arg(qlonglong a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char(' ')) const
QString arg(QStringView a, int fieldWidth = 0, QChar fillChar = QLatin1Char(' ')) const
QString arg(QLatin1String a, int fieldWidth = 0, QChar fillChar = QLatin1Char(' ')) const
QString arg(const QString &a1, const QString &a2) const
QString arg(const QString &a1, const QString &a2, const QString &a3) const
QString arg(const QString &a1, const QString &a2, const QString &a3, const QString &a4) const
QString arg(const QString &a1, const QString &a2, const QString &a3, const QString &a4, const QString &a5) const
QString arg(const QString &a1, const QString &a2, const QString &a3, const QString &a4, const QString &a5, const QString &a6) const
QString arg(const QString &a1, const QString &a2, const QString &a3, const QString &a4, const QString &a5, const QString &a6, const QString &a7) const
QString arg(const QString &a1, const QString &a2, const QString &a3, const QString &a4, const QString &a5, const QString &a6, const QString &a7, const QString &a8) const
QString arg(const QString &a1, const QString &a2, const QString &a3, const QString &a4, const QString &a5, const QString &a6, const QString &a7, const QString &a8, const QString &a9) cons

🌰举例子:

QString strOtherAdd("strOtherAdd");
qDebug() << strOtherAdd.fill('z');
qDebug() << strOtherAdd.fill('A', 2);
qDebug(" ");

qDebug() << strOtherAdd.setNum(1);
qDebug() << strOtherAdd.setNum(32,16);  // 设置为 32 的十六进制打印值
qDebug() << strOtherAdd.setNum(2.1);
qDebug(" ");

qDebug() << strOtherAdd.sprintf("%s_%d","strOtherAdd", 111);
qDebug(" ");

qDebug() << QString("%1 拼接第三个参数 %3,再拼接第二个参赛 %2").arg("strOtherAdd").arg(2.1).arg(3);
qDebug(" ");

🎯运行结果:
在这里插入图片描述


在这里插入图片描述

🎄三、QString对象的 清空、 删除

本小节介绍从 对象删除一段字符串、清空整个QString对象等操作。

✨3.1 清空字符串

清除字符串的内容并设置为 null,执行这个函数后,调用isNULL()会返回true

void clear()

🌰举例子:

QString strClear("strClear");
strClear.clear();
qDebug() << strClear;			// ""
qDebug() << strClear.isNull();	// true
qDebug(" ");

✨3.2 从字符串末尾删除字符

void chop(int n)	// 从字符串末尾删除n个字符。
QString chopped(int len) const	// 返回一个子字符串,其中包含size() - len这个字符串最左边的字符。

🌰举例子:

// 从字符串末尾删除字符
QString strChop("strChop");
strChop.chop(2);
qDebug() << strChop;        // strCh
qDebug() << strChop.chopped(1); // strC

✨3.3 删除指定字符串、指定位置的子字符串

相关函数原型如下:

QString &remove(int position, int n)	// 从给定的位置索引开始,从字符串中删除n个字符,并返回对字符串的引用。
QString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive)	//删除此字符串中每次出现的字符ch,并返回对该字符串的引用。
QString &remove(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &remove(const QRegExp &rx)
QString &remove(const QRegularExpression &re)

🌰举例子:

QString strClearDel("strClearDel");
qDebug() << strClearDel.remove("Del");  // strClear
qDebug() << strClearDel.remove(0,3);    // Clear
qDebug(" ");

✨3.4 删除空白字符

simplified:删除字符串首尾的空白字符,并且将字符串内部的空白字符替换为空格;
trimmed:只删除字符串首尾的空白字符,其他不变。

// 返回一个字符串,该字符串从开始和结束处删除了空白,并将每个内部空白序列替换为单个空格。
// 空白是指QChar::isSpace()返回true的任何字符。这包括ASCII字符“\ t”,' \ n ', ' \ v ', ' \ f ', ' \ r ',和' '。
QString simplified() const

// 删除开始、结束处空白的字符串。
// 空白是指QChar::isSpace()返回true的任何字符。这包括ASCII字符“\ t”,' \ n ', ' \ v ', ' \ f ', ' \ r ',和' '。
QString trimmed() const

🌰举例子:

// 删除空白字符
QString strSpace(" str  S  pace ");
qDebug() << strSpace.simplified();  // str S pace
qDebug() << strSpace.trimmed();     // str  S  pace

✨3.5 截断字符串

// 在给定位置索引处截断字符串。
// 如果指定的位置索引超出字符串的末尾,则不会发生任何事情。
void truncate(int position)

🌰举例子:

// 截断字符串
QString strTruncate("strTruncate");
strTruncate.truncate(3);
qDebug() << strTruncate;    // str

在这里插入图片描述

🎄四、修改 字符串

✨4.1 修改字符串大小

将字符串的大小设置为size字符。
如果size大于当前大小,则扩展字符串以使其长度为字符长,并将额外的字符添加到末尾。新字符未初始化。
如果size小于当前大小,则从末尾删除字符。

void resize(int size)
void resize(int size, QChar fillChar)

🌰举例子:

QString strResize("strResize");
strResize.resize(6);
qDebug() << strResize;      // "strRes"
strResize.resize(16, '*');
qDebug() << strResize;      // "strRes**********"

✨4.2 替换掉字符串的字符

将从索引位置开始的n个字符替换为后面的字符串,并返回对该字符串的引用。
注意:如果指定的位置索引在字符串范围内,但position + n超出了字符串范围,则n将被调整为停止在字符串末尾。

QString &replace(int position, int n, const QString &after)
QString &replace(int position, int n, const QChar *unicode, int size)
QString &replace(int position, int n, QChar after)
QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &replace(const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &replace(QLatin1String before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &replace(QLatin1String before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &replace(QChar ch, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &replace(QChar c, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QString &replace(const QRegExp &rx, const QString &after)
QString &replace(const QRegularExpression &re, const QString &after)

🌰举例子:

// 替换掉字符串的字符
QString strReplace("strReplace");
qDebug() << strReplace.replace(0, 3, "STR");    // "STRReplace"
qDebug() << strReplace.replace(0, 3, '*');      // "*Replace"
qDebug() << strReplace.replace('e', 'E');       // "*REplacE"

✨4.3 交换字符串

// 将字符串other与此字符串交换。这个操作非常快,从不失败。
void swap(QString &other)

🌰举例子:

// 交换字符串
QString strSwap("strSwap");
QString strSwapWhat("strSwapWhat");
strSwap.swap(strSwapWhat);
qDebug() << strSwap;	// "strSwapWhat"

✨4.3 字符串格式转换

// 格式转换
CFStringRef toCFString() const
QString toCaseFolded() const
double toDouble(bool *ok = nullptr) const
float toFloat(bool *ok = nullptr) const
QString toHtmlEscaped() const
int toInt(bool *ok = nullptr, int base = 10) const
QByteArray toLatin1() const
QByteArray toLocal8Bit() const
long toLong(bool *ok = nullptr, int base = 10) const
qlonglong toLongLong(bool *ok = nullptr, int base = 10) const
QString toLower() const
NSString *toNSString() const
short toShort(bool *ok = nullptr, int base = 10) const
std::string toStdString() const
std::u16string toStdU16String() const
std::u32string toStdU32String() const
std::wstring toStdWString() const
uint toUInt(bool *ok = nullptr, int base = 10) const
ulong toULong(bool *ok = nullptr, int base = 10) const
qulonglong toULongLong(bool *ok = nullptr, int base = 10) const
ushort toUShort(bool *ok = nullptr, int base = 10) const
QVector<uint> toUcs4() const
QString toUpper() const
QByteArray toUtf8() const
int toWCharArray(wchar_t *array) const

在这里插入图片描述

🎄五、从字符串 查询

✨5.1 查询字符串的字符个数

// 返回此字符串中的字符数。相当于size()。
int length() const
int size() const

🌰举例子:

QString strQuerySize("strQuerySize");
qDebug() << strQuerySize.length();	// 12
qDebug() << strQuerySize.size();	// 12

✨5.2 查询字符串的一个字符

// 获取字符串中给定位置的字符,position的范围:0 <= position < size()
const QChar at(int position) const

// [] 运算符,返回指定位置的字符
QCharRef operator[](int position)
const QChar operator[](int position) const
const QChar operator[](uint position) const
QCharRef operator[](uint position)

// 返回字符串的最后一个字符,等同于at(size()-1)
QChar back() const
QCharRef back()

// 返回字符串的第一个字符,等同于at(0)
QChar front() const
QCharRef front()

// 返回一个指向存储在QString中的数据的指针。指针可用于访问和修改组成字符串的字符。
QChar *data()
const QChar *data() const

🌰举例子:

// 查询字符串的一个字符
QString strQueryChar("strQueryChar");
qDebug() << strQueryChar.at(1);     // t
qDebug() << strQueryChar[2];        // r
qDebug() << strQueryChar.back();    // r
qDebug() << strQueryChar.front();   // s
QChar *pCharArr = strQueryChar.data();
for(int i=0; i<strQueryChar.length(); i++)
{
qDebug() << pCharArr[i];
}
qDebug(" ");

🎯运行结果:
在这里插入图片描述


✨5.3 查询字符串是否为空、是否为大小写

bool isEmpty() const	// 如果字符串没有字符则返回true;否则返回false。
bool isLower() const	// 如果字符串只包含小写字母则返回true,否则返回false。
bool isNull() const		// 如果此字符串为空,则返回true;否则返回false。
bool isRightToLeft() const	// 如果从右向左读取字符串,则返回true。
bool isUpper() const	// 如果字符串只包含大写字母则返回true,否则返回false。

isEmpty 、isNull的区别:isEmpty是查询除了\0之外,还有没有字符;isNull是只有在没有任何字符时才有true。

🌰举例子:

QString strQueryEmpty("strqueryempty");
qDebug() << "QString(\"\").isEmpty() is " << QString("").isEmpty();
qDebug() << "QString(\"\").isNull() is " << QString("").isNull();
qDebug() << strQueryEmpty.isLower();
qDebug() << strQueryEmpty.isUpper();

🎯运行结果:
在这里插入图片描述


✨5.4 查询是否包含指定的 子字符串

// 如果此字符串包含字符串str,则返回true;否则返回false。
bool contains(const QString &str, Qt::CaseSensitivity cs = ...) const
bool contains(QChar ch, Qt::CaseSensitivity cs = ...) const
bool contains(QLatin1String str, Qt::CaseSensitivity cs = ...) const
bool contains(const QStringRef &str, Qt::CaseSensitivity cs = ...) const
bool contains(const QRegExp &rx) const
bool contains(QRegExp &rx) const
bool contains(const QRegularExpression &re) const
bool contains(const QRegularExpression &re, QRegularExpressionMatch *match) const

// 如果字符串以 子字符串s 开头返回true;否则返回false。
bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool startsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool startsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool startsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

// 如果字符串以 子字符串s 结尾返回true;否则返回false。
bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool endsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool endsWith(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
bool endsWith(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

🌰举例子:

QString strContains("strContains");
qDebug() << strContains.contains("str");
qDebug() << strContains.contains('C');
qDebug() << strContains.startsWith("str");
qDebug() << strContains.startsWith('s');
qDebug() << strContains.endsWith("ins");
qDebug() << strContains.endsWith('s');

✨5.5 查询 子字符串 的位置

// 从 QString 对象查询子字符串的位置并返回,成功的话,返回值为位置,失败返回-1
int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int indexOf(const QRegExp &rx, int from = 0) const
int indexOf(QRegExp &rx, int from = 0) const
int indexOf(const QRegularExpression &re, int from = 0) const
int indexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const

// 返回字符串str在此字符串中最后出现的索引位置,从索引位置向后搜索。
// 如果from为-1(默认),则从最后一个字符开始搜索;如果from为-2,则在倒数第二个字符处,依此类推。如果没有找到str返回-1。
int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int lastIndexOf(const QRegExp &rx, int from = -1) const
int lastIndexOf(QRegExp &rx, int from = -1) const
int lastIndexOf(const QRegularExpression &re, int from = -1) const
int lastIndexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const

🌰举例子:

QString strIndex("strIndexIn");
qDebug() << strIndex.indexOf("Index");   // 3
qDebug() << strIndex.indexOf('I');       // 3
qDebug() << strIndex.indexOf("In", 4);   // 8
qDebug() << strIndex.lastIndexOf("Index");   // 3
qDebug() << strIndex.lastIndexOf('I');       // 8
qDebug() << strIndex.lastIndexOf("In");      // 8

✨5.6 查询 子字符串 的出现的个数

// 返回字符串str在此字符串中出现的次数(可能重叠)。
int count(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int count() const
int count(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
int count(const QRegExp &rx) const
int count(const QRegularExpression &re) const

🌰举例子:

// 查询 子字符串 的出现的个数
QString strCount("strCountCoCo");
qDebug() << strCount.count("Co");	// 3
qDebug() << strCount.count();		// 12

✨5.7 获取一个 子字符串

// 返回包含字符串最左边n个字符的子字符串。
QString left(int n) const
QStringRef leftRef(int n) const
QString leftJustified(int width, QChar fill = QLatin1Char(' '), bool truncate = false) const // 返回大小为width的字符串,其中包含由填充字符填充的此字符串。

// 返回一个包含此字符串的n个字符的字符串,从指定的位置索引开始。
QString mid(int position, int n = -1) const
QStringRef midRef(int position, int n = -1) const

// 返回包含字符串最右n个字符的子字符串。如果n大于等于size()或小于零,则返回整个字符串。
QString right(int n) const
QString rightJustified(int width, QChar fill = QLatin1Char(' '), bool truncate = false) const
QStringRef rightRef(int n) const

// 这个函数返回字符串的一部分。
// 该字符串被视为由字符sep分隔的字段序列。返回的字符串由从位置开始到位置结束(包括位置结束)的字段组成。如果未指定end,则包括从位置开始到字符串末尾的所有字段。字段编号如果从左开始计数为0、1、2等,如果从右到左计数为-1、-2等,。
QString section(QChar sep, int start, int end = ..., QString::SectionFlags flags = SectionDefault) const
QString section(const QString &sep, int start, int end = -1, QString::SectionFlags flags = SectionDefault) const
QString section(const QRegExp &reg, int start, int end = -1, QString::SectionFlags flags = SectionDefault) const
QString section(const QRegularExpression &re, int start, int end = -1, QString::SectionFlags flags = SectionDefault) const

🌰举例子:

// 获取一个 子字符串
QString strSubString("strSubString");
qDebug() << strSubString.left(6);       // strSub
qDebug() << strSubString.leftRef(7);    // strSubS
qDebug() << strSubString.leftJustified(15, QChar('*'));   // strSubString***
qDebug() << strSubString.mid(6,6);      // String
qDebug() << strSubString.midRef(9,3);   // ing
qDebug() << strSubString.right(6);      // String
qDebug() << strSubString.rightRef(7);   // bString
qDebug() << strSubString.rightJustified(15, QChar('*'));   // ***strSubString

QString strPath("opt/etc/init.d/tftp/export");
qDebug() << strPath.section('/', 1, 3); // 以 / 为分隔,取第2个到第4个之间的字符串 etc/init.d/tftp
qDebug() << strPath.section('/', -2, -1); // 以 / 为分隔,取倒数第2个到倒数第1个之间的字符串 etc/init.d/tftp

🎯运行结果:
在这里插入图片描述


✨5.8 将字符串分割成多个 子字符串

// 在sep出现的地方将字符串拆分为子字符串,并返回这些字符串的列表。如果sep在字符串中的任何地方都不匹配,split()返回一个包含该字符串的单元素列表。
// Cs指定sep匹配是区分大小写还是不区分大小写。
//如果 behavior 是QString::SkipEmptyParts,空条目不会出现在结果中。缺省情况下,保留空表项。
QStringList split(const QString &sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QStringList split(QChar sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QStringList split(const QRegExp &rx, QString::SplitBehavior behavior = KeepEmptyParts) const
QStringList split(const QRegularExpression &re, QString::SplitBehavior behavior = KeepEmptyParts) const

// 在sep出现的地方将字符串拆分为子字符串引用,并返回这些字符串的列表。
QVector<QStringRef> splitRef(const QString &sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QVector<QStringRef> splitRef(QChar sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QVector<QStringRef> splitRef(const QRegExp &rx, QString::SplitBehavior behavior = KeepEmptyParts) const
QVector<QStringRef> splitRef(const QRegularExpression &re, QString::SplitBehavior behavior = KeepEmptyParts) const

🌰举例子:

// 拆分字符串
QString strSplite("opt/etc/init.d/tftp/export");
QStringList strSpliteList = strSplite.split("/");
for(int i=0; i<strSpliteList.size(); i++)
{
	qDebug() << strSpliteList[i];
}
qDebug(" ");

🎯运行结果:
在这里插入图片描述


在这里插入图片描述

🎄六、其他

写太多了,几乎翻译了Qt文档里的内容,其他的以后用到再补充吧!!!

// 根据给定版本的Unicode标准,以给定的Unicode规范化模式返回字符串。
QString normalized(QString::NormalizationForm mode, QChar::UnicodeVersion version = QChar::Unicode_Unassigned) const

// 比较s1和s2,如果s1小于、等于或大于s2,则返回一个小于、等于或大于0的整数。
int localeAwareCompare(const QString &other) const
int localeAwareCompare(const QStringRef &other) const

在这里插入图片描述
如果文章有帮助的话,点赞👍、收藏⭐,支持一波,谢谢 😁😁😁


http://www.kler.cn/news/317370.html

相关文章:

  • Gitlab runner的使用示例(二):Maven + Docker 自动化构建与部署
  • 【游戏引擎】C++自制游戏引擎 Lunar Game Engine
  • 基于vue框架的宠物销售管理系统3m9h3(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
  • 微软开源GraphRAG的使用教程-使用自定义数据测试GraphRAG
  • Java中的快速排序算法详解
  • c++ pair
  • ubuntu下检查端口是否占用问题,编写shell脚本检查端口是否占用
  • 使用Python实现图形学曲线和曲面的NURBS算法
  • ChartLlama: A Multimodal LLM for Chart Understanding and Generation论文阅读
  • unity Compute Shaders 使程序在GPU中运行
  • LeetCode54. 螺旋矩阵(2024秋季每日一题 21)
  • 计算机毕业设计Hadoop+PySpark深圳共享单车预测系统 PyHive 共享单车数据分析可视化大屏 共享单车爬虫 共享单车数据仓库 机器学习 深度学习
  • 工博会蓝卓逛展攻略
  • C#测试调用Ghostscript.NET浏览PDF文件
  • <刷题笔记> 二叉搜索树与双向链表注意事项
  • OpenHarmony(鸿蒙南向开发)——标准系统方案之瑞芯微RK3568移植案例(上)
  • 流域碳中和技术
  • 使用Docker一键部署Blossom笔记软件
  • 【C#生态园】一文详解:NHibernate、Entity Framework Core、Dapper 等 .NET ORM 框架优劣对比
  • M9410A VXT PXI 矢量收发信机,300/600/1200MHz带宽
  • 防火墙详解(三)华为防火墙基础安全策略配置(命令行配置)
  • 11. DPO 微调示例:根据人类偏好优化LLM大语言模型
  • 【电商搜索】现代工业级电商搜索技术-Ha3搜索引擎平台简介
  • 应用层-网络协议
  • Java面试篇基础部分- Java中的阻塞队列
  • 解决selenium爬虫被浏览器检测问题
  • 5. 条件 Conditionals
  • 56 mysql 用户权限相关的实现
  • Spring高手之路24——事务类型及传播行为实战指南
  • DHCP中继工作原理