a) 组合字符串
QString str1 = "Welcome";
str1 = str1 + " to you!";
QString str2 = "Hello";
str2 += " World!";
str2.append(str1);
QString str3;
str3.sprintf("%s","Welcome");
str3.sprintf("%s","to you! ");
str3.sprintf("%s%s","welcome ","to you!");
QString str4;
str4 = QString("%1 was bornin %2. %3").arg("John").arg(1982).arg("Nice to meetyou");
//移除字符串两边的空白字符
QStringstr5 = " Welcome \t to \nyou! ";
str5 = str5.trimmed();
//移除字符串两边的空白字符,使用单个空格字符代替字符串中出现的空白字符
str5=str5.simplified();
b) 查询字符串数据
//判断一个字符串是否以某个字符串开头
QString str6 = "Welcome to you!";
if(true == str6.startsWith("Welcome",Qt::CaseSensitive))
qDebug()<<"true"<<endl;
//判断一个字符串是否以某个字符串结尾
if(true == str6.endsWith('!'))
qDebug()<<"true"<<endl;
//判断一个指定的字符串是否出现过
if(true== str6.contains("Welcome"))
qDebug()<<"true"<<endl;
c) 字符串转换
//QString::toInt()将字符串转换为整形数值,类似的函数还有toDouble()/toFloat()/toLong()/toLongLong()等
QString str7 = "123";
bool ok;
int hex = str7.toInt(&ok,16);//转换成功则ok=true, hex=293
int dec = str7.toInt(&ok,10);//转换成功则ok=true, dec=125
d) 与QbyteArray的转换
QString str8 = " Welcome toyou! ";
QByteArray ba =str8.toLocal8Bit();//返回一个ASCII编码的8位字符串
qDebug()<<ba;
ba = str8.toLatin1();//返回一个UTF-8编码的8位字符串(UTF-8是ASCII码的超集,它支持整个Unicode字符集)
qDebug()<<ba;
ba.append("Hello,World!");
qDebug()<<ba.data();
toLacal8Bit()返回一个系统本地编码的8位字符串