QString
QString s1 = "abc";
QString s2("hello");
//字符串连接
QString s = s1 + s2;
//组包
s = QString("a = %1, b = %2, c = %3").arg(250).arg("hello").arg(22.22);
qDebug()<<s;
//输出结果:a = 250, b = hello, c = 22.22
s = "a=250,b=hello,c=22.22";
//参数表示按逗号分隔, 拆分出第0段到第1段, 保存到tmp
QString tmp = s.section(",", 0, 1);
qDebug()<<"tmp = "<<tmp;
//输出结果:tmp="a=250"
tmp = tmp.section("=", 1, 1);
qDebug()<<"tmp="<<tmp;
//输出结果:tmp="250";
//QString转int
int a = tmp.toInt();
qDebug()<<"a="<<a;
//输出结果:a=250;
//int转QString组包方式转
tmp = QString("%1").arg(123);
qDebug()<<"tmp="<<tmp;
//输出结果:tmp="123"
//int转QString函数方式转
tmp = QString::number(234);
//char *转QString
const char * buf = "hello, world";
QString ss = buf;
qDebug()<<"ss = "<< ss;
//字符数组转QString
char buf2[100] = "hello world";
ss = buf2;
qDebug()<<"ss = "<<ss;
//QString转char *
ss = "hello abc";
char * buf3 = ss.toLocal8Bit().data();//转换为本地编码
char * buf4 = ss.toUtf8().data();//转换为utf-8编码
QStringList
//定义
QStringList list;
//添加字符串
list << "hello";
list << "world";
list.append("abc");
//打印
qDebug()<< "list ="<< list;
//遍历
for(int i = 0; i< list.size();++i)
{
QString tmp = list.at(i);
qDebug()<<"tmp ="<< tmp;
}
//使用QStringList拆分QString
QString str = "hello,world,abc,qt";
QStringList list2 = str.split(",");
qDebug()<<"list2="<<list2;
//输出:list2=("hello","world","abc","qt");
//判断QStringList中是否包含某个成员
bool b = list2.contains("123");
//移除QStringList中的成员
list2.removeOne("abc");
原文地址:https://www.cnblogs.com/nonsupport/p/8571163.html
时间: 2024-10-13 20:32:08