首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >QT基础数据类型-QString

QT基础数据类型-QString

原创
作者头像
Swing Dunn
发布2025-12-03 15:02:59
发布2025-12-03 15:02:59
900
举报
文章被收录于专栏:Qt_5_FQt_5_F
代码语言:txt
复制
#include <QCoreApplication> //Qt提供的一个事件循环

#include<QDebug>
#include<QDateTime>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //1:QString 提供二元“+”操作符, 功能一样“+=”
    QString str1 = "Hello ";
    str1 = str1 + "World";
    qDebug()<<str1; //qDeBUG() 打印信息 但是带双引号
    qDebug()<<qPrintable(str1); //打印信息,去掉双引号

    QString str2 = "I am ";
    str2 += "Dunn";    //+= 操作符 追加字符串
    qDebug()<<qPrintable(str2);

    //2:QString::append()  追加字符串
    QString str3 = "Good ";
    QString str4 = "Morning";
    str3.append(str4);
    qDebug()<<qPrintable(str3);

    //3:组合字符串  QString::sprintf()
    QString str5 ;
    str5.sprintf("%s %d %s", "When I wake up at", 6, "alock");
    qDebug()<<qPrintable(str5);

    //4:QString::arg() 字符串组合方式  该函数可以重载处理多种数据类型,因为它类型安全,同时支持Unicode,可以改变%n参数顺序
    QString str6;
    str6 = QString("%1 was born in %2").arg("I").arg(2000);
    qDebug()<<qPrintable(str6);

    //QString还支持其他字符串组合方式 如insert(), prepend(), replace()

    //----! QString查询字符串方式
    //1:Qstring::startwith()函数  字符串以某字串开头
    //对于的QString::endWith() 以某字串结尾
    QString str7 = "My birthday";
    qDebug()<< str7.startsWith("my",Qt::CaseInsensitive); //true 大小写不敏感
    qDebug()<< str7.startsWith("my",Qt::CaseSensitive); //false 大小写敏感

    //2: QString::contains() 判断指定字符串是否出现过(被包含)
    qDebug()<<str7.contains(" bir",Qt::CaseSensitive); //true 包含

    //3:QString::toInt()函数 转换成整数
    QString str8 = "25e";
    bool isloop;
    int hex = str8.toInt(&isloop, 16); //str8按16进制转换成整数  isloop 能否转换 true 返回转换后的数  false 返回0
    qDebug()<<"isloop: "<< isloop <<" , " << hex;
    //"25e" 作为16进制数转换成整数 2*16^2 + 5 *16^1 + 14 *16^0 = 512 + 80 + 14 = 606

    //4:QString::compare() 字符串比较函数
    int a1 = QString::compare("abcd","ABCD",Qt::CaseInsensitive); //0  大小不敏感时  相等
    int b1 = QString::compare("abcd","ABCD",Qt::CaseSensitive);//32 大小写敏感 'a' 比'A'的ASCII码值大32
    int c1 =QString::compare("abcd","Cat",Qt::CaseInsensitive);//-2 大小写不敏感 'a'比 'c'的ASCII码值小2
    qDebug()<<"a: "<<a1 <<" b " << b1;

    //5:Qt将QString转换成对应的ASCII码
    QString str9 = "ABC abc";
    QByteArray bytes = str9.toUtf8();
    for(int i = 0; i < bytes.size(); i++)
        qDebug()<< int(bytes.at(i));

    //二: Qt常见的基本数据类型 定义在#include<QtGlobal>中
    // QDateTime QByteArray

    QDateTime dt;
    QString strDate =dt.currentDateTime().toString("yyyy-MM-dd hh::mm::ss") ;
    qDebug()<<qPrintable(strDate);

    QByteArray arr1("Qt Creator!");
    QByteArray arr2 = arr1.toLower();
    qDebug()<<arr2;
    QByteArray arr3 = arr2.toUpper();
    qDebug()<<arr3;
    return a.exec();
}

所有用例运行结果

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档