QString的使用

该文介绍了QString类在Qt中的使用,包括将字符串转换为各种数值类型,如int、long、double等,以及进行进制转换。同时,还提到了QString的一些常见操作,如append、prepend用于字符串拼接,toUpper、toLower转换大小写,以及indexOf、endsWith、startsWith等搜索和检查功能。
  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端的字符串

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值