您的位置:首页 > 其它

操作字符串

2016-06-07 13:53 246 查看
//组合两个字符串“+”,“+=”
QString str1="Welcome";
str1=str1+"to you!";    //str1="Welcome to you!"

QString str2="Hello, ";
str2+="World!";         //str2="Hello,World!"

//QString::append()追加字符串
QString str1="Welcome ";
QString strw="to";
str1.append(str2);  //str1="Welcome to"
str1.append("you!");    //str1="Welcome to you!"

//QString::sprintf()输出字符串
QString str;
str.sprintf("%s","Welcome");            //str="Welcome"
str.sprintf("%s","to you!");            //str="to you!"
str.sprintf("%s","Welcome","to you!")   //str="Welcome to you!

//QString::arg()函数,功能更强:
//支持多数据类型、
//设置字段宽度
//数字基数
//浮点数精度
QString str;
str=QString("%1 was born in %2.").arg("John").arg(1992);//str="John was born in 1992."

insert()函数:在原字符串特定的位置插入另一个字符串
prepend()函数:在原定字符串开头的位置插入另一个字符串
replase()函数:用指定的字符串代替原字符串中的某些字符

QString::trimmed():移除字符串两端的空白字符
QString::simplified():移除字符串两端的空白字符,使用单个空格字符“ ”代替字符串中出现的空白字符
*空白字符包括回车"\n",换行字符"\r",制表符"\t"和空格字符" "等
QString str="Welcome \t to \n you!    ";
str=str.trimmed();      //str="Welcome \t to \n you!"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: