QT学习(4)

QT学习(4)

QT查询字符串数据

(1)QString::startsWith()函数判断一个字符串是否以某个字符串开头,此函数具有两个参数,第一个参数指向一个字符串,第二个参数指定是否大小写敏感(默认情况下,是大小写敏感的)。

QString str = "Welcome to you";
str.startsWith("Welcome",Qt::CaseSensitive); //true  Qt::CaseInsensitive  大小写不敏感
str.startsWith("you",Qt::CaseSensitive); //false

(2)QString::endsWith()函数,判断一个字符串是否以某个字符串结束。
(3)QString::contains()函数,判断一个指定的字符串是否出现过。

    QString str = "Welcome to you";
    bool flags = str.contains("welcome",Qt::CaseInsensitive); //true

(4)比较两个字符串

operator<(const QString&)
operator<=(const QString&)
operator==(const QString&)
operator>=(const QString&)
operator>(const QString&)

字符串转换

(1)QString::toInt()函数将字符串转换为整数值,类似的还有toDouble(),toFloat(),toLong(),toLongLong()。

    QString str = "125";
    bool ok;
    int hex = str.toInt(&ok,16);  //293  ok=true
    int dec = str.toInt(&ok,10);  //125  ok=true

将字符串转换为整数,QString::toInt()函数有两个参数,第一个参数是一个bool类型的指针,用于返回转换的状态,转换成功时设置为true,转换失败设置为false。第二个参数设置了转换的基数。

除此之外,类似的操作还有toAscii()、toLatin1()、toUtf8()、tolocal8Bit()。

NULL字符串和空(empty)字符串的区别

一个NULL字符串一定是一个空字符串,而一个空字符串未必是一个NULL字符串

    qDebug()<<QString().isNull();  //true
    qDebug()<<QString().isEmpty(); //true
    qDebug()<<QString("").isNull(); //false
    qDebug()<<QString("").isEmpty(); //true
  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值