操作字符串
字符串拼接的五种方法
/******************************操作字符串******************************/
/***first***/
QString str1="welcome ";//str1="welcome"
str1=str1+"to you! ";//str1="welcome to you!"
qDebug("str1=%s\n",qPrintable(str1));
/***second***/
QString str2="Hellow! ";//str2="Hellow! "
str1.append(str2);//str1="welcome to you! Hellow! "
str1.append("you! ");//str1="welcome to you! Hellow! you! "
qDebug("str1=%s\n", qPrintable(str1));
/***third***/
QString str3;
str3.sprintf("%s", " welcome ");//str3= " welcome "
str3.sprintf("%s", " to you! ");//str3=" welcome to you! "
str3.sprintf("%s %s", "Welcome", "to you! ");//str3="Welcome to you! "
qDebug("str3=%s\n", qPrintable(str3));
/***fourth***/
QString str4;
str4=QString("%1 was born in %2.").arg("John").arg(1982);//str="John was born in 1982"
qDebug("str4=%s\n", qPrintable(str4));
字符串规范
/***字符串规范***/
QString str5=" Welcome \t to \n you ";
qDebug("str5=%s\n",qPrintable(str5));
qDebug("str5.trimmed()=%s\n", qPrintable(str5.trimmed()));//移除字符串两端的空格
qDebug("str5.simplified()=%s\n", qPrintable(str5.simplified()));//移除字符串两端的空格,并使用" "代替字符串中出现的空白字符或者换行
查询字符串数据
/*****************************查询字符串数据******************************/
QString str="Welcome to you! ";
/***查询开头***/
bool b=str.startsWith("welcome", Qt::CaseSensitive);//大小写敏感(输出false)
b=str.startsWith("welcome", Qt::CaseInsensitive);//大小写不敏感(输出true)
b=str.startsWith("you", Qt::CaseSensitive);//大小写敏感(输出false)
/***查询结尾***/
b=str.endsWith("you! ",Qt::CaseSensitive);//输出true
/***查询此字符串是否出现过***/
b=str.contains("Welcome", Qt::CaseSensitive);//输出true
字符串转换
/******************************字符串转换**********************************/
QString str6="125";
bool ok;
int hex=str6.toInt(&ok, 16);//把125当作16进制转化为int型的10进制293,转化成功则ok为true
qDebug("%d\n", hex);//输出293