标签:spl dex alt one contain clu mamicode upper case
//append()在字符串后面添加文字 QString str1="qt ",str2="Creator ",str3,str4,str5;
str3=str1;
str4=str1.append(str2);
str5=str3.prepend(str2);
qDebug()<<"str4"<<str4;
qDebug()<<"str5"<<str5;
运行结果:
//toUpper()将字符串字母全部转换为大写字母
//toLower()将字符串字母全部转换为小写字母
QString str1="qt ",str2="CreaTor ",str3,str4;
str3=str1.toUpper();
str4=str2.toLower();
qDebug()<<"str3"<<str3;
qDebug()<<"str4"<<str4;
运行结果:
// count()、size()、length()都返回字符串的字符个数,字符中若是有汉字,一个汉字算一个字符。
QString str1="qt",str2="CreaTor",str3,str4;
int n;
n=str1.count();
qDebug()<<"n1"<<n;
n=str2.size();
qDebug()<<"n2"<<n;
n=str2.length();
qDebug()<<"n3"<<n;
运行结果:
QString str1="qt",str2="汉字",str3,str4;
int n;
n=str2.size();
qDebug()<<"n2"<<n;
n=str2.length();
qDebug()<<"n3"<<n;
运行结果:
//trimmed()去掉字符串首尾的空格
//simplified()去掉首尾空格,并去掉中间多余的空格,保留一个空格
QString str1=" Are you OK? ",str2;
QString str3=" Are you OK? ",str4;
str2=str1.trimmed();
qDebug()<<"str2"<<str2;
str4=str3.simplified();
qDebug()<<"str4"<<str4;
运行结果:
//indexOf("o")查找字符第一次出现的位置
//lastIndexOf("o")查找字符最后一次出现的位置
QString str1="Are you OKo?";
int n;
n=str1.indexOf("o");
qDebug()<<"n1="<<n;
n=str1.lastIndexOf("o");
qDebug()<<"n2="<<n;
运行结果:从0开始计数
//contains()判断字符串是否在某个字符串中,返回False或True
QString str1="Are you OK?";
int n;
n=str1.contains("o",Qt:: CaseInsensitive);//不区分大小写
qDebug()<<"n1="<<n;
n=str1.contains("o",Qt::CaseSensitive);//区分大小写
qDebug()<<"n2="<<n;
//endsWith判断字符串是否以某个字符串结束,返回False或True
//startsWith判断字符串是否以某个字符串开头,返回False或True
QString str1="Are you OK?o";
int n;
n=str1.endsWith("o",Qt::CaseInsensitive);//不区分大小写
qDebug()<<"n1="<<n;
n=str1.endsWith("o",Qt::CaseSensitive);//区分大小写
qDebug()<<"n2="<<n;
n=str1.startsWith("A",Qt::CaseSensitive);//区分大小写
qDebug()<<"n3="<<n;
//left()从字符串中取左侧多少个字符
//right()从字符串中取右侧多少个字符
QString str1="Qt,是一个跨平台的,C++ 开发库",str2,str3;
int n;
n=str1.indexOf(",");
str2=str1.left(n);
qDebug()<<"str2="<<str2;
n=str1.lastIndexOf(",");
str3=str1.right(str1.size()-n-1);
qDebug()<<"str3="<<str3;
//其他操作
QString str1 ="hello",str3,str4;
str1=str1+" world!!";
qDebug()<<"str1:"<<str1;
QString str2 ="hello";
str2+=" kitty!!";
qDebug()<<"str2:"<<str2;
qDebug()<<str3.sprintf("%s","welcome ");
qDebug()<<str3.sprintf("%s %s","welcome ","to you");
str4=QString("%1 to %2").arg("how").arg("you");
qDebug()<<"str4:"<<str4;
QString str = "a,,b,c";
QStringList list1 = str.split(‘,‘);
// list1: [ "a", "", "b", "c" ]
qDebug()<<"list1:"<<list1;
QStringList list2 = str.split(‘,‘, QString::SkipEmptyParts);
// list2: [ "a", "b", "c" ]
qDebug()<<"list2:"<<list2;
//字符串转换
//把QString转换为int类型
QString str5="333";
bool ok;
int hex=str5.toInt(&ok,16);
int dec=str5.toInt(&ok,10);
qDebug()<<"hex:"<<hex;
qDebug()<<"dec:"<<dec;
//把QString转换为float类型
QString str6="333.45";
float d=str6.toFloat();
qDebug()<<"d:"<<d;
//把QString转换为double类型
QString str7="333.45";
double c=str7.toDouble();
qDebug()<<"c:"<<c;
//数字转换为字符串
int n=100;
QString str11 = QString::number(n);
qDebug()<<"str11:"<<str11;
foreach用法:
#include <QLinkedList>
QLinkedList<QString> list;
list << "one" << "two" << "three";
// list: ["one", "two", "three"]
QString str;
foreach(str,list)
{
qDebug()<<str;
}
标签:spl dex alt one contain clu mamicode upper case
原文地址:https://blog.51cto.com/14165014/2452886