QString - 查找字符串数据 + 转化

QString - 查找字符串数据

QString::startsWith

void startsWith_test() // 判断是否以某个字符串开头
{
    QString str = "Welcome to you! ";
    // 第一个参数 :指定字符串
    // 第二个参数 :指定大小写是否敏感
    str.startsWith("Welcome", Qt::CaseInsensitive); // 返回 true
    str.startsWith("you", Qt::CaseInsensitive);     // 返回 false
}

QString::endsWith

void endsWith_test()
{
    QString str = "Welcome to you!";
    // 第一个参数 :指定字符串
    // 第二个参数 :指定大小写是否敏感
    qDebug() << str.endsWith("Welcome", Qt::CaseInsensitive); // 返回 false
    qDebug() << str.endsWith("you!", Qt::CaseInsensitive);    // 返回 true
}

QString::compare

compare(const QString &, const QString &, Qt::CaseSensitivity);

该函数可以指定是否进行大小写比较,而大小写比较是完全基于Unicode编码值的。

------------------------------------------------------------------------------------------------

字符串的转化

void change_int()
{
    QString str = "125";
    bool ok; // 用于设置是否转换成功
    // 第二个参数,指定转换的基数
    /**
     * 0x - 基数16
     * 0 - 基数8
     * 其他情况基数是 10
     */
    int hex = str.toInt(&ok, 16); // ok = true,hex = 293
    int dec = str.toInt(&ok, 10); // ok = true,dec = 125
    qDebug() << hex;
    qDebug() << dec;
}

void exchange_test()
{
    QString str = " welcome to you!  "; // 初始化一个字符串对象
    QByteArray ba = str.toUtf8();
    qDebug() << ba;
    ba.append("Hello world! ");
    qDebug() << ba.data();
}

    QString提供的字符编码集的转换函数将会返回一个const char*类型版本的 QByteArray 即构造函数QByteArray(const char*)构造的QByteArray对象。
    QByteArray类具有一个字节它既可以存储原始字节 (raw bytes),也可以存储传统的以“0”结尾的 8 位的字符串。在Qt中,使用QByteArray 比使用const char*更方便,且QByteArray 也支持隐式共享。

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

void Null_or_Empty()
{
    QString().isNul1();    // true
    Ostring().isEmpty();   // true
    OString("").isNull();  // false
    QString("").isEmpty(); // true
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值