QString的使用

  1. 字符串转为整数的函数

int toInt(bool ok = Q NULLPTR , int base = 10) const
long toLong (bool ok = Q NULLPTR, int base = 10) const 
short toShort (bool *ok = Q NULLPTR, int base = 10) const 
unint toUint (bool *ok = Q NULLPTR , int base = 10) const 
unlong  toULong (bool *ok = Q_NULLPTR nt base = 10) const
double toDouble(bool ok = Q NULLPTR) const 
float toFloat (bool ok = Q NULLPTR) const
  1. 保留两位小数:

str=QString : :number(total ,’ f ’, 2); 
str=QStr ng : asprintf .2f total);
str=str. setNum (total ,’ f ’, 2); 
str=str.sprintf 2f ”, total);
//total为数值变量
  1. 进制转换:

str=QString::umber(val 16)  //转换为十六进制的字符串--方法一
str=str setNum(val 16) ; //十六进制--方法二

bool ok;
int val=str.toInt(&ok,2);//以二进制读入字符串
  1. 常用功能:

//append()在字符串的后面添加字符
//prepend()在字符串的前面添加字符
QString str1="我";
QString str2="爱";
QString str3=str1;
str3.append(str2);
str2.prepend(str1);

//toUpper())将字符串内的字母全部转换为大写形式   
//toLower() ()将宇母全部转换为小写形式
QString strl="Hello, World", str2; 
str2=strl.toUpper(); //strl="HELLO , WORLD"
str2=strl.toLower(); //strl="hello, world"

//count())返回字符串的个数
//size()返回字符串的个数   
//length()返回字符串的个数

trimmed()去掉字符串首尾的空格
simplified()去掉首尾的空格,中间连续的空格也用一个空格替换

indexOf()在自身字符串内查找参数字符串str出现的位置
lastlndexOf()是查找某个字符串最后出现的位置

isNull()判读字符串是否为空
isEmpty()判读字符串是否为空

contains()判断字符串是否包含某个字符串,可区分大小写

endsWith()判断是否以某个字符串结束
startsWith()判断是否以某个字符串开头

left()从字符串中取左边多少个字符
right()从字符串中取右边多少个字符

section()从字符串中提取以sep作为分隔符,从start端到end端的字符串