本文最后更新于 103 天前,其中的信息可能已经有所发展或是发生改变。
在此之前需要配置Qt环境:Qt环境配置
数据转换
QString str1 = "123";
QString str2 = "456";
// 数据转换 to int
int a1 = str1.toInt();
int a2 = str2.toInt();
qDebug() << a1+a2;
// 数字转QString
int a3 = 520; // 208h
QString str3 = QString::number(a3,16); // 转为16进制
qDebug() << str3;
字符串拼接
// 字符串拼接
QString str4 = "hello";
QString str5 = "_";
QString str6 = "world";
qDebug() << str4 + str5 + str6; // "+"被重载了
转换大小写
//转换大小写
QString str7 = "Qt";
QString str8 = str7.toLower();
qDebug() << str8;
QString str9 = str4.toUpper();
qDebug() << str9;
获取字符串长度
//判断字符长度: count/size/length
int len1 = str4.count();
int len2 = str4.size();
int len3 = str4.length();
//长度为5
qDebug() << len1;
qDebug() << len2;
qDebug() << len3;
空格整理
//去除首尾空格 trimmed
QString str10 = " OK OK OK ";
qDebug() << str10;
QString str11 = str10.trimmed();
qDebug() << str11;
//统一空格格式
QString str12 = " OK OK OK ";
QString str13 = str12.simplified();
qDebug() << str13;
查找字符串元素
//获取文件名
QString str14 = "C:\\software\\IDA_Pro_v8.3_Portable\\ida64.exe";
int last = str14.lastIndexOf("\\"); //截取最后一个'\'之后的字符元素下标,由于'\'是转义字符,所以此处打两个
int exe = str14.indexOf(".exe"); //截取.exe之前的字符元素下标,这样就能获取文件名了
qDebug() << last << " " << exe; //打印文件下标,分别是[33]和[39]
判断字符串是否为空
//判断字符串是否为空: isNull,isEmpty
QString str15;
QString str16 = "";
qDebug() << str15.isNull() << " " << str15.isEmpty(); //结果都返回true
qDebug() << str16.isNull() << " " << str16.isEmpty(); //前者返回false,即包含'\0'
判断是否包含某字符串
//判断是否包含某个字符串
QString str17 = "Fuck you";
//是否包含Fuck
int index = str17.contains("fuck",Qt::CaseInsensitive); //包含则返回1,否则返回0
//CaseSensitive区分大小写,CaseInsensitive不区分大小写
if(index){
str17 = "F**k you";
}
qDebug() << str17;
判断字符串以某前缀开头/后缀结尾
//是否以特定后缀结尾 ".exe" and ".elf"
QString pe = "test.exe";
bool isexe = pe.endsWith(".exe",Qt::CaseSensitive);
if(isexe)
qDebug() << "It is a PE project.";
else
qDebug() << "It isn't a PE project.";
//是否以特定字符串开头
QString elf = "test.elf";
bool istest = elf.startsWith("test",Qt::CaseInsensitive);
if(istest)
qDebug() << "O测试程序O";
else
qDebug() << "@非测试@";
完整代码
#include <QCoreApplication>
#include <QDebug>
#include <QString>
/*
QString 采用uniocde编码,每一个字符是一个16位的Qchar,即可兼容中文
*/
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString str1 = "123";
QString str2 = "456";
// 数据转换 to int
int a1 = str1.toInt();
int a2 = str2.toInt();
qDebug() << a1+a2;
// 数字转QString
int a3 = 520; // 208h
QString str3 = QString::number(a3,16); // 转为16进制
qDebug() << str3;
// 字符串拼接
QString str4 = "hello";
QString str5 = "_";
QString str6 = "world";
qDebug() << str4 + str5 + str6; // "+"被重载了
//转换大小写
QString str7 = "Qt";
QString str8 = str7.toLower();
qDebug() << str8;
QString str9 = str4.toUpper();
qDebug() << str9;
//判断字符长度: count/size/length
int len1 = str4.count();
int len2 = str4.size();
int len3 = str4.length();
//长度为5
qDebug() << len1;
qDebug() << len2;
qDebug() << len3;
//去除首尾空格 trimmed
QString str10 = " OK OK OK ";
qDebug() << str10;
QString str11 = str10.trimmed();
qDebug() << str11;
//统一空格格式
QString str12 = " OK OK OK ";
QString str13 = str12.simplified();
qDebug() << str13;
//获取文件名
QString str14 = "C:\\software\\IDA_Pro_v8.3_Portable\\ida64.exe";
int last = str14.lastIndexOf("\\"); //截取最后一个'\'之后的字符元素下标,由于'\'是转义字符,所以此处打两个
int exe = str14.indexOf(".exe"); //截取.exe之前的字符元素下标,这样就能获取文件名了
qDebug() << last << " " << exe; //打印文件下标,分别是[33]和[39]
//判断字符串是否为空: isNull,isEmpty
QString str15;
QString str16 = "";
qDebug() << str15.isNull() << " " << str15.isEmpty(); //结果都返回true
qDebug() << str16.isNull() << " " << str16.isEmpty(); //前者返回false,即包含'\0'
//判断是否包含某个字符串
QString str17 = "Fuck you";
//是否包含Fuck
int index = str17.contains("fuck",Qt::CaseInsensitive); //包含则返回1,否则返回0
//CaseSensitive区分大小写,CaseInsensitive不区分大小写
if(index){
str17 = "F**k you";
}
qDebug() << str17;
//是否以特定后缀结尾 ".exe" and ".elf"
QString pe = "test.exe";
bool isexe = pe.endsWith(".exe",Qt::CaseSensitive);
if(isexe)
qDebug() << "It is a PE project.";
else
qDebug() << "It isn't a PE project.";
//是否以特定字符串开头
QString elf = "test.elf";
bool istest = elf.startsWith("test",Qt::CaseInsensitive);
if(istest)
qDebug() << "O测试程序O";
else
qDebug() << "@非测试@";
return a.exec();
}