1.QString 常用方法
//append 在后面添加字符串,prepend在前面添加字符串
QString s1 = "张";
QString s2 = "三";
QString s3 = s1;
s1.append(s2); //s1 = "张三"
s3.prepend(s2); //s3 = "三张"
//toUpper,toLower转换大小写
QString s4 = "Hello,World";
QString s5,s6;
s5 = s4.toUpper(); //HELLO,WORLD
s6 = s4.toLower(); //hello,world
//字符串的个数
int nCount = s4.count(); //nCount = 11
nCount = s4.size(); //nCount = 11
nCount = s4.length(); //nCount = 11
//trimmed去掉字符串首尾的空格,simplified不仅去掉首尾的空格,中间连续的空格也用一个空格代替
QString s7 = " QString S7 Are you OK ? ";
QString s8,s9;
s8 = s7.trimmed(); //s8 = "QString S7 Are you OK ?"
s9 = s7.simplified(); //s9 = "QString S7 Are you OK ?"
//indexOf查找字符串首次出现的位置,没找到返回-1,lastIndexOf最后一次出现的位置
QString s10 = "Are you eOK";
nCount = s10.indexOf("you"); //4
nCount = s10.lastIndexOf("e"); //8
//判断是否为空,只有未赋值的字符串isNull返回true
QString s11,s12 = "";
bool bRet = s11.isNull(); //true
bRet = s11.isEmpty(); //true
bRet = s12.isNull(); //false
bRet = s12.isEmpty(); //true
//判断是否包含字符串
s10 = "Are you eOK";
bRet = s10.contains("you",Qt::CaseInsensitive); //不区分大小写 true
bRet = s10.contains("YOu",Qt::CaseSensitive); //区分大小写 false
//判断是否以字符串结尾,字符串开始
s10 = "Are you eOK";
bRet = s10.endsWith("eOK",Qt::CaseInsensitive); //不区分大小写 true
bRet = s10.endsWith("EoK",Qt::CaseInsensitive); //区分大小写 false
bRet = s10.startsWith("a"); //缺省为不区分大小写true
//从左边取多少个字符,从右边取多少个字符
QString x = "Pineapple";
QString y1 = x.left(4); // y == "Pine"
QString y2 = x.right(5); // y == "apple"
//从字符串中提取 以第一个参数作为分隔符 ,从第二个参数 到 第三个参数的字符串。
QString str;
QString csv = "forename,middlename,surname,phone";
QString path = "/usr/local/bin/myapp"; // First field is empty
str = csv.section(',', 2, 2); // str == "surname"
str = path.section('/', 3, 4); // str == "bin/myapp"
str = path.section('/', 3, 3, QString::SectionSkipEmpty); // str == "myapp"
//分割字符串
QStringList list1 = path.split("/");
//字符串 -> 数字
int number = QString("23").toInt();
//数字 -> 字符串
QString strNum = QString::number(23);