2、QString
QString::append()
将字符串末尾添加另一个字符串
QString str1 ="Good"; QString str2= "Sutdent"; str1.append(str2);
拼接字符串
int aa=99; int aa=99; double b=89.7; char c[10]="gec"; QString str1=QString("%1@%2@%3").arg(aa).arg(b).arg(c); QString str2=QString("%2@%1@%3").arg(aa).arg(b).arg(c); //打印结果 和123的顺序有关 aa为1 b为2 c为3 qDebug()<<"str1 = "<<str1<<" str2 = "<<str2;
QString::sprintf()
QString strtemp; strtemp.sprintf("%s","Hello "); qDebug() << qPrintable(strtemp); strtemp.sprintf("%s","Hello World."); qDebug() << qPrintable(strtemp); strtemp.sprintf("%s %s","welcome","to you"); qDebug() << qPrintable(strtemp);
查询字符串
QString temp="abcdefg"; //判断字符串是否以该字符串开头 Qt::CaseInsensitive代表大小写不敏感 默认是敏感的 qDebug() << temp.startsWith("Ab",Qt::CaseInsensitive); //true //大小写敏感 qDebug() << temp.startsWith("Ab",Qt::CaseSensitive); // false //对应 qDebug() << temp.endsWith("fg",Qt::CaseInsensitive);//true //判断指定字符串是否出现过 qDebug() << temp.contains("cd",Qt::CaseInsensitive);//true
//查询字符串在帝即位出现
int position = temp.indexOf("d");
转换字符串类型
//转换类型 toInt toFloat ... QString tt="25"; bool isloop; int ret3=tt.toInt(); int ret4=tt.toInt(&isloop); int hex=tt.toInt(&isloop,16); //isloop=true hex=37 2*16 + 5 //把123当成是八进制的25,然后转换成10进制数 qDebug()<< hex << ret3 << ret4 ;
字符串比较
//字符串比较 QString str1="123a"; QString str2="123A"; int aa=QString::compare(str1,str2,Qt::CaseInsensitive); //大小写不敏感 =0 int bb=QString::compare(str1,str2,Qt::CaseSensitive); //大小写敏感 =32 qDebug() << aa<< bb;
整数转换为字符串
int a3=123; QString sa3=QString::number(a3); QString sa31=QString::number(a3,16); //转换为16位字符串 qDebug() << sa3 <<sa31;
分割字符串
QString split="2021-01-01"; QStringList mylist=split.split("-"); for (int i = 0; i < mylist.count(); i++) { // qDebug() << mylist[i]; qDebug() << mylist.at(i); }
截取字符串
//截取子串 QString str4="hellogecworldchina"; QString otherstr=str4.mid(8,5); // 如果你理解为下表从0开始,那第八位刚好是截取的第一位 qDebug()<<"otherstr = "<<otherstr;
时间格式处理
QDateTime dt=QDateTime::currentDateTime(); QString formattedDateTime = dt.toString("yyyy-MM-dd hh:mm:ss"); // 将时间格式化为指定格式的字符串 qDebug() << qPrintable(formattedDateTime);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)