Qt C++ 的QString用法,QString非常常用,必不可少

QString的方法也是围绕着 增删改查的本质。

1.创建 QString 对象:

QString str1 = "Hello, World!";
QString str2("Another String");
QString str3 = QString("Formatted String: %1 %2").arg(42).arg("Qt");

2.获取字符串长度:

QString str1 = "Hello, World!";
int length = str1.length(); // 或者使用 str1.size();
qDebug() << length;

//输出 13

3.访问字符串中的字符:

QString str1 = "Hello, World!";


QChar firstChar = str1.at(0); // 获取第一个字符
QChar lastChar = str1.at(str1.length() - 1); // 获取最后一个字符
qDebug() << firstChar;
qDebug() << lastChar;

输出
H
!

4.字符串连接:

QString result = str1 + " " + str2; // 使用运算符+
result.append(" Appended String"); // 使用 append 方法

5.字符串比较:

QString str1 = "Hello, World!";
QString str2 = "Hello, World!";

if (str1 == str2)
{
      qDebug() << u8"相等";

}

int compareResult = str1.compare(str2);
qDebug() << compareResult;

输出
 相等
  0

6.字符串查找:

 QString str1 = "Hello, World!";


 int index = str1.indexOf("World"); // 查找子字符串的起始索引
 bool contains = str1.contains("Hello"); // 判断是否包含子字符串
 qDebug() << index;    // 7
 qDebug() << contains;  // true

7.字符串截取

 在QString中,可以使用mid方法来截取一段指定长度的字符串。mid方法接收两个参数,第一个参数是待截取的字符串的起始位置,第二个参数是待截取字符串的长度。


QString str = "Hello, World!";
// 从字符串的第7个字符开始,截取5个字符,newStr的值为"World"
QString newStr = str.mid(7, 5);
qDebug() << newStr ; // "World"

从字符串的第7个字符开始,截取之后的所有字符

QString str = "Hello, World!";
QString newStr = str.mid(7); 
qDebug() << newStr ;  //newStr的值为"World!"

从字符串左或右截取

QString str = "Hello, World!";

// 从字符串的左侧开始,截取5个字符
QString newStr1 = str.left(5); 
// 从字符串的右侧开始,截取6个字符
QString newStr2 = str.right(6); 
qDebug() << newStr1 ; //newStr1的值为"Hello"
qDebug() << newStr2 ;  //newStr2的值为"World!"

8.字符串替换:

将"World"替换成"Mars",str的值变为"Hello, Mars!"

QString str = "Hello, World!";
str.replace("World", "Mars");

如果需要在替换前先判断一下字符串中是否包含指定的子串,可以使用contains方法。

QString str = "Hello, World!";
if (str.contains("World")) {
    str.replace("World", "Mars");
}

9.字符串分割:

按","为分隔符分割字符串,list的值为["apple", "banana", "pear"]

QString str = "apple,banana,pear";
QStringList list = str.split(","); 

按","为分隔符分割字符串,返回2个子串,list的值为["apple", "banana,pear"]

QString str = "apple,banana,pear";
QStringList list = str.split(",", 2);

10.字符串删除:

一种是使用remove方法,先删除指定位置及之后的字符,再删除指定位置及之前的字符,即可得到所需的子串。

QString str = "Hello, World!";
str.remove(str.indexOf(','), str.length()); // 删除","及其后的字符
str.remove(0, str.lastIndexOf(' ') + 1); // 删除第一个空格及其前面的字符

11.字符串索引:

另一种是使用section方法,可以方便地从复杂字符串中提取子串。section方法接收三个参数,第一个参数是指定的分隔符,第二个参数是子串的索引(从0开始),第三个参数是标志位,表示截取的方式。

QString str = "China,Beijing,Chaoyang";
QString city = str.section(',', 1, 1); // 返回第2个子串"Beijing"

在QString中,可以使用indexOf和lastIndexOf方法查找指定字符串的位置。这两个方法都接收两个参数,第一个参数是待查找的字符串,第二个参数是查找起点的位置。


QString str = "Hello, World!";
int pos1 = str.indexOf("l"); // 查找"l"第一次出现的位置,pos1的值为2
int pos2 = str.lastIndexOf("l"); // 查找"l"最后一次出现的位置,pos2的值为10
int pos3 = str.indexOf("l", pos2 + 1); // 从pos2+1的位置开始查找"l",pos3的值为3

// 返回"l"在字符串中出现的次数,count的值为3

QString str = "Hello, World!";
int count = str.count("l"); // 返回"l"在字符串中出现的次数,count的值为3

12.字符转换

在Qt中经常需要将char*、std::string等其他类型的字符串转换成QString类型,QString提供了fromStdString、fromLocal8Bit和fromUtf8等方法来实现。

char* cstr = "Hello, World!";
QString str1 = QString::fromLocal8Bit(cstr); // 将本地编码的字符串转换成QString
std::string stdstr = "Hello, World!";
QString str2 = QString::fromStdString(stdstr); // 将std::string类型的字符串转换成QString
const char* utf8str = "你好,世界!";
QString str3 = QString::fromUtf8(utf8str); // 将UTF-8编码的字符串转换成QString

大小写转换

QString lowerStr = str1.toLower();
QString upperStr = str1.toUpper();

  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值