1.WHAT
//创建字符串对象 QString s1("hello"); //使用字符常量构造:const char * QString s2 = "qt"; //使用赋值构
//运算符重载,拼接字符串 QString s3 =s1+s2; qDebug() <<"s3="<<s3; //s3= "hello qt" s3 += "!"; //或者+=QString对象 qDebug() <<"s3="<<s3; //s3= "hello qt!" //append方式追加 QString s4 = "hello"; s4.append(" world!"); //使用append方式追加 qDebug() <<"s4="<<s4; //s4= "hello world!
//sprintf方式实现:很少用 QString s10; s10.sprintf("%s","Welcom1e"); qDebug() <<"s10="<<s10; //s10= "Welcom1e" s10.sprintf("%s","to you!"); qDebug()<<"s10="<<s10; //s10= "to you!" s10.sprintf("%s %s","Welcome","to you!"); qDebug() <<"s10="<<s10; //s10= "Welcome to you!"
//QString::arg(); QString s20 = "yongheng0"; int n = 852; QString s21 = QString("%1.%2%4.%3").arg("www").arg(s20).arg("com").arg(n); qDebug()<<"s21="<<s21; //s21= "www.yongheng0852.com" //等价于 QString s22 = "www." +s20 + QString::number(n)+ ".com"; qDebug() <<"s22="<<s22; //s22= "www.yongheng0852.com"
//insert()在字符串的任意位置插入一个字符串 QString s30 = "yongheng.com"; QString s31 = "0852"; s30.insert(8,&s31); //在字符串第8个位置插入s31这个字符串 qDebug() <<"s30="<<s30; //s30= "yongheng0852.com"
s30.prepend("www."); qDebug() <<"s30="<<s30; //s30= "www.yongheng0852.com"
s30.replace("www","http://www"); //把www替换为http方式 qDebug()<<"s30="<<s30; //s30= "http://www.yongheng0852.com"
//trimmed()移除两端的空白字符 QString s40 = " www.\tyongheng0852.\ncom "; qDebug()<<"s40="<<s40; //s40= " www.\tyongheng0852.\ncom " s40 = s40.trimmed(); //返回一个去除空格的对象 qDebug() <<"s40="<<s40; //s40= "www.\tyongheng0852.\ncom"
//simplified()移除两端空表字符和中间的特殊转移字符(用空格替换) QString s41 = " hello\nworld\t! "; qDebug()<<"s41="<<s41; //s41= " hello\nworld\t! " s41 = s41.simplified(); qDebug()<<"s41="<<s41; //s41= "hello world !"
QString s50 = "www.yongheng0852.com"; qDebug()<<s50.startsWith("www."); //默认区分大小写,返回true qDebug()<<s50.startsWith("wWw."); //默认区分大小写,返回false qDebug()<<s50.startsWith("wWw",Qt::CaseSensitive); //区分大小写.返回false qDebug()<<s50.startsWith("wWw",Qt::CaseInsensitive); //忽略大小写,返回true---------②endWith(); 以某个字符串结尾(和startWith用法类似)
qDebug() << s50.endsWith("coM",Qt::CaseInsensitive); //忽略大小写,返回true
qDebug()<<s50.contains("yongHENG",Qt::CaseSensitive); //区分大小写:false qDebug()<<s50.contains("yongHENG",Qt::CaseInsensitive); //不区分大小写:true
QString s60 = "cabc"; //重载比较运算符,返回bool qDebug() << (s60 < "b"); //false qDebug() << (s60 <= "b"); //false qDebug()<< (s60 == "b"); //false qDebug()<< (s60 > "b"); //true qDebug() << (s60 >= "b"); //true qDebug() << (s60 < "d"); //true
int ret = 0; ret =QString::localeAwareCompare(s60,"abcdef"); //ret= 1 qDebug()<<"ret="<<ret; ret =QString::localeAwareCompare(s60,"cabc"); //ret= 0 qDebug() <<"ret="<<ret; ret =QString::localeAwareCompare(s60,"gabcef"); //ret= -1 qDebug() <<"ret="<<ret;
ret = QString::compare("bcd","bcd"); //默认区分大小写:0 qDebug() <<"ret="<<ret; ret = QString::compare("bcd","Bcd"); //默认区分大小写:32 qDebug() <<"ret="<<ret; ret = QString::compare("bcd","Bcd",Qt::CaseSensitive); //区分大小写:32 qDebug() <<"ret="<<ret; ret = QString::compare("bcd","Bcd",Qt::CaseInsensitive); //不区分大小写:0 qDebug()<<"ret="<<ret;
QString s70 = "0168"; int a70 = s70.toInt(); qDebug()<<"a70="<<a70; //a70= 168 s70 = "0.168"; double a71 = s70.toDouble(); qDebug() <<"a71="<<a71; //a71= 0.168 s70 = "hello qt"; a70 = s70.toInt(); qDebug()<<"a70="<<a70; //转换失败,数据为0
bool ok = false; s70 = "0168"; a70 = s70.toInt(&ok,16); //指定字符串中的数据是16进制,把他转换为10进制 if(ok) { qDebug()<<"s70 = 0168:"<<"16进制转换10进制成功,数据是:"<<a70; //数据是:360 } else { qDebug()<<"s70 = 0168:"<<"16进制转换10进制失败!"; } s70 = "0167"; //如果数据超过了8进制的范围,则转换失败 a70 = s70.toInt(&ok,8); if(ok) { qDebug()<<"s70 = 0167:"<<"8进制转换10进制成功,数据是:"<<a70; //数据是:119 } else { qDebug()<<"s70 = 0167:"<<"8进制转换10进制失败!"; } s70 = "hello qt"; a70 = s70.toInt(&ok); //不指定第二个参数默认转换为10进制 if(ok) { qDebug()<<"s70 = hello qt :"<<"转换成功,数据是:"<<a70; } else { qDebug()<<"s70 = hello qt :"<<"转换失败!"; //数据转换失败 }
int b = 100; QString s80 = QString("%1").arg(b); qDebug() <<"s80="<<s80; //s80= "100"
b = 200; s80 = QString::number(b); qDebug()<<"s80="<<s80; //s80= "200" double d = 0.852; s80 = QString::number(d); qDebug() <<"s80="<<s80; //s80= "0.852"
//QString 转C++ string: QString q_str1 = "hello"; std::string c_str1 = q_str1.toStdString(); std::cout<<"QString 转C++ string="<<c_str1<<std::endl; //C++ string 转QString: std::string c_str2 = "world"; QString q_str2 = QString::fromStdString(c_str2); qDebug()<<"C++ string 转QString="<<q_str2;
QString s81 = QString(); qDebug()<<s81.isNull(); //true qDebug()<<s81.isEmpty(); //true QString s82 = QString(""); qDebug()t<<s82.isNull(); //false qDebug()<<s82.isEmpty(); //true qDebug()<<"以下代码和上面等价:"; qDebug()<<QString().isNull(); //true qDebug()<<QString().isEmpty(); //true qDebug()<<QString("").isNull(); //false qDebug()<<QString("").isEmpty(); //true
#include "widget.h" #include<QString> #include<QDebug> #include <iostream> #include <string> //c++的string类 Widget::Widget(QWidget *parent) : QWidget(parent) { //创建字符串对象-------------------------------------- QString s1("hello"); //使用字符常量构造:const char * QString s2 = " qt"; //使用赋值构造 qDebug()<<"--------------运算符重载,拼接字符串--------------"; QString s3 =s1+s2; qDebug() <<"s3="<<s3; //s3= "hello qt" s3 += "!"; //或者+=QString对象 qDebug() <<"s3="<<s3; //s3= "hello qt!" //append方式追加 QString s4 = "hello"; s4.append(" world!"); //使用append方式追加 qDebug() <<"s4="<<s4; //s4= "hello world!" qDebug()<<"--------------sprintf方式实现:很少用--------------"; QString s10; s10.sprintf("%s","Welcom1e"); qDebug() <<"s10="<<s10; //s10= "Welcom1e" s10.sprintf("%s","to you!"); qDebug() <<"s10="<<s10; //s10= "to you!" s10.sprintf("%s %s","Welcome","to you!"); qDebug() <<"s10="<<s10; //s10= "Welcome to you!" qDebug()<<"--------------组包:QString::arg()--------------"; QString s20 = "yongheng0"; int n = 852; QString s21 = QString("%1.%2%4.%3").arg("www").arg(s20).arg("com").arg(n); qDebug() <<"s21="<<s21; //s21= "www.yongheng0852.com" //等价于 QString s22 = "www." +s20 + QString::number(n)+ ".com"; qDebug() <<"s22="<<s22; //s22= "www.yongheng0852.com" qDebug()<<"--------------其他组合字符串的方式--------------"; //insert()在字符串的任意位置插入一个字符串 QString s30 = "yongheng.com"; QString s31 = "0852"; s30.insert(8,&s31); //在字符串第8个位置插入s31这个字符串 qDebug() <<"s30="<<s30; //s30= "yongheng0852.com" //prepend()在字符串的开头位置插入一个字符串 s30.prepend("www."); qDebug() <<"s30="<<s30; //s30= "www.yongheng0852.com" qDebug()<<"--------------replace()字符串替换--------------"; s30.replace("www","http://www"); //把www替换为http方式 qDebug() <<"s30="<<s30; //s30= "http://www.yongheng0852.com" qDebug()<<"--------------trimmed()移除两端的空白字符--------------"; QString s40 = " www.\tyongheng0852.\ncom "; qDebug() <<"s40="<<s40; //s40= " www.\tyongheng0852.\ncom " s40 = s40.trimmed(); //返回一个去除空格的对象 qDebug() <<"s40="<<s40; //s40= "www.\tyongheng0852.\ncom" //simplified()移除两端空表字符和中间的特殊转移字符(用空格替换) QString s41 = " hello\nworld\t! "; qDebug() <<"s41="<<s41; //s41= " hello\nworld\t! " s41 = s41.simplified(); qDebug() <<"s41="<<s41; //s41= "hello world !" //查询字符串数据----------------------------------------- qDebug()<<"--------------以某个字符串开头或结尾:s50--------------"; QString s50 = "www.yongheng0852.com"; qDebug()<<s50.startsWith("www."); //默认区分大小写,返回true qDebug()<<s50.startsWith("wWw."); //默认区分大小写,返回false qDebug()<<s50.startsWith("wWw",Qt::CaseSensitive); //区分大小写.返回false qDebug()<<s50.startsWith("wWw",Qt::CaseInsensitive); //忽略大小写,返回true //以某个字符串结尾(和startWith用法类似) qDebug() << s50.endsWith("coM",Qt::CaseInsensitive); //忽略大小写,返回true //判断是否包含某个字符串 qDebug()<<"判断是否包含某个字符串:s50"; qDebug()<<s50.contains("yongHENG",Qt::CaseSensitive); //区分大小写:false qDebug()<<s50.contains("yongHENG",Qt::CaseInsensitive); //不区分大小写:true //比较字符串大小:以ascii码的大小比较,a<z---------------------------- qDebug()<<"--------------比较字符串大小:s60--------------"; QString s60 = "cabc"; //重载比较运算符,返回bool qDebug() << (s60 < "b"); //false qDebug() << (s60 <= "b"); //false qDebug() << (s60 == "b"); //false qDebug() << (s60 > "b"); //true qDebug() << (s60 >= "b"); //true qDebug() << (s60 < "d"); //true //使用本地字符集locale比较,返回整型,大于返回正整数,等于返回0,小于返回负整数 int ret = 0; ret =QString::localeAwareCompare(s60,"abcdef"); //ret= 1 qDebug() <<"ret="<<ret; ret =QString::localeAwareCompare(s60,"cabc"); //ret= 0 qDebug() <<"ret="<<ret; ret =QString::localeAwareCompare(s60,"gabcef"); //ret= -1 qDebug() <<"ret="<<ret; //可指定区分大小写的比较,如果不相等,返回值和localeAwareCompare的返回值方式类似 ret = QString::compare("bcd","bcd"); //默认区分大小写:0 qDebug() <<"ret="<<ret; ret = QString::compare("bcd","Bcd"); //默认区分大小写:32 qDebug() <<"ret="<<ret; ret = QString::compare("bcd","Bcd",Qt::CaseSensitive); //区分大小写:32 qDebug() <<"ret="<<ret; ret = QString::compare("bcd","Bcd",Qt::CaseInsensitive); //不区分大小写:0 qDebug() <<"ret="<<ret; //字符串转换---------------------------- qDebug()<<"--------------字符串->数据(int等)--------------"; QString s70 = "0168"; int a70 = s70.toInt(); qDebug() <<"a70="<<a70; //a70= 168 s70 = "0.168"; double a71 = s70.toDouble(); qDebug() <<"a71="<<a71; //a71= 0.168 s70 = "hello qt"; a70 = s70.toInt(); qDebug() <<"a70="<<a70; //转换失败,数据为0 //对转换结果进行判断----------------------- bool ok = false; s70 = "0168"; a70 = s70.toInt(&ok,16); //指定字符串中的数据是16进制,把他转换为10进制 if(ok) { qDebug()<<"s70 = 0168:"<<"16进制转换10进制成功,数据是:"<<a70; //数据是:360 } else { qDebug()<<"s70 = 0168:"<<"16进制转换10进制失败!"; } s70 = "0167"; //如果数据超过了8进制的范围,则转换失败 a70 = s70.toInt(&ok,8); if(ok) { qDebug()<<"s70 = 0167:"<<"8进制转换10进制成功,数据是:"<<a70; //数据是:119 } else { qDebug()<<"s70 = 0167:"<<"8进制转换10进制失败!"; } s70 = "hello qt"; a70 = s70.toInt(&ok); //不指定第二个参数默认转换为10进制 if(ok) { qDebug()<<"s70 = hello qt :"<<"转换成功,数据是:"<<a70; } else { qDebug()<<"s70 = hello qt :"<<"转换失败!"; //数据转换失败 } //数字转换为字符串 qDebug()<<"--------------数字转换为字符串--------------"; int b = 100; QString s80 = QString("%1").arg(b); qDebug() <<"s80="<<s80; //s80= "100" b = 200; s80 = QString::number(b); qDebug() <<"s80="<<s80; //s80= "200" double d = 0.852; s80 = QString::number(d); qDebug() <<"s80="<<s80; //s80= "0.852" //QString 转C++ string: QString q_str1 = "hello"; std::string c_str1 = q_str1.toStdString(); std::cout<<"QString 转C++ string="<<c_str1<<std::endl; //C++ string 转QString: std::string c_str2 = "world"; QString q_str2 = QString::fromStdString(c_str2); qDebug()<<"C++ string 转QString="<<q_str2; qDebug()<<"--------------NULL字符串和空字符串的区别--------------"; QString s81 = QString(); qDebug()<<s81.isNull(); //true qDebug()<<s81.isEmpty(); //true QString s82 = QString(""); qDebug()<<s82.isNull(); //false qDebug()<<s82.isEmpty(); //true qDebug()<<"以下代码和上面等价:"; qDebug()<<QString().isNull(); //true qDebug()<<QString().isEmpty(); //true qDebug()<<QString("").isNull(); //false qDebug()<<QString("").isEmpty(); //true } Widget::~Widget() { }
--------------运算符重载,拼接字符串-------------- QString 杞珻++ string=hello //标准c++的字符串在qt中的编码问题导致乱码 s3= "hello qt" s3= "hello qt!" s4= "hello world!" --------------sprintf方式实现:很少用-------------- s10= "Welcom1e" s10= "to you!" s10= "Welcome to you!" --------------组包:QString::arg()-------------- s21= "www.yongheng0852.com" s22= "www.yongheng0852.com" --------------其他组合字符串的方式-------------- s30= "yongheng0852.com" s30= "www.yongheng0852.com" --------------replace()字符串替换-------------- s30= "http://www.yongheng0852.com" --------------trimmed()移除两端的空白字符-------------- s40= " www.\tyongheng0852.\ncom " s40= "www.\tyongheng0852.\ncom" s41= " hello\nworld\t! " s41= "hello world !" --------------以某个字符串开头或结尾:s50-------------- true false false true true 判断是否包含某个字符串:s50 false true --------------比较字符串大小:s60-------------- false false false true true true ret= 1 ret= 0 ret= -1 ret= 0 ret= 32 ret= 32 ret= 0 --------------字符串->数据(int等)-------------- a70= 168 a71= 0.168 a70= 0 s70 = 0168: 16进制转换10进制成功,数据是: 360 s70 = 0167: 8进制转换10进制成功,数据是: 119 s70 = hello qt : 转换失败! --------------数字转换为字符串-------------- s80= "100" s80= "200" s80= "0.852" C++ string 转QString= "world" --------------NULL字符串和空字符串的区别-------------- true true false true 以下代码和上面等价: true true false true
---------④完整的代码(注意解压后不能有中文路径)
------1)主要的原因,在 Qt中使用了标准c++的string类时,由于编码问题,很多中文的字符串,在qt中显示出来的都是乱码,使用QTstring,处理编码问题很方便。
评论