码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Qt字符串类应用与常用基本数据类型


    目录

    一.Qt 字符串类应用

    1、常用操作字符串

    1.1 QString 提供一个二元的“+”操作符,主要用于组合两个字符串。QString str1'"Hello world"传递给 QString 一个 const char*类型的 ASCII 字符串“Hello world”,它被解释为一个典型的以“\0”结尾的 C 类型字符串

    1.2 QString::append()函数具备与“+=”操作符同样的功能,直接在一个字符串末尾添加另一个字符串。

    1.3 组合字符串: QString::sprintf ()。其实它跟 C++库当中sprintf()函数一样

    1.4 字符串组合方式 QString::arg()函数,该函数的重载可以处理多种数据类型。因为它类型安全,同时支持 Unicode,可以改变%n 参数顺序。

    2、常用查询字符串

    2.1 函数 QString::startswith()判断一个字符串是否以某个字符串开头。Qt::CaseInsensitive 代表大小写不敏感; Qt::CaseSensitive表示大小定敏感。对应关系函数:QString::endsWith()。

    2.2 函数 QString::contains (判断一个指定的字符串是否出现过)

    2.3 QString::toInt()函数将字符串转换为整型数值   toDouble () /toFloat () /toLong()等等

    2.4 QString::compare ( 函数对两个字符串进行比较)

    2.5 Qt将QString 转换成 ASCII码

    二、Qt 常见基本数据类型(注意:定义在#include )

    QDateTime

    QByteArray


    一.Qt 字符串类应用

    1、常用操作字符串
    1.1 QString 提供一个二元的“+”操作符,主要用于组合两个字符串。QString str1'"Hello world"传递给 QString 一个 const char*类型的 ASCII 字符串“Hello world”,它被解释为一个典型的以“\0”结尾的 C 类型字符串
    1. #include //Qt提供一个事件循环
    2. #include //输出流
    3. int main(int argc, char *argv[])
    4. {
    5. QCoreApplication a(argc, argv);
    6. //1.1 QString 提供二元+操作符运用,功能与+=一样
    7. QString str1 = "zgl";
    8. str1 = str1+"nb";
    9. qDebug()<//打印信息
    10. qDebug()<<qPrintable(str1); //去双引号
    11. QString str2 = "666";
    12. str2 = str2 +"zgl";
    13. qDebug()<
    14. qDebug()<<qPrintable(str2);
    15. return a.exec();
    16. }
    1.2 QString::append()函数具备与“+=”操作符同样的功能,直接在一个字符串末尾添加另一个字符串。
    1. //1.2 QString::append()函数
    2. QString str1 = "zgl";
    3. QString str2 = "nb";
    4. str1.append(str2);
    5. qDebug()<
    6. qDebug()<<qPrintable(str1);
    1.3 组合字符串: QString::sprintf ()。其实它跟 C++库当中sprintf()函数一样
    1. //1.3 QString::sprintf()函数
    2. QString strtemp;
    3. strtemp.sprintf("%s","hello zgl");
    4. qDebug()<<qPrintable(strtemp);
    5. strtemp.sprintf("%s","hello ljx");
    6. qDebug()<<qPrintable(strtemp);
    7. strtemp.sprintf("%s %s","zgl","ljx"); //空格bian表示字符串间距
    8. qDebug()<<qPrintable(strtemp);
    1.4 字符串组合方式 QString::arg()函数,该函数的重载可以处理多种数据类型。因为它类型安全,同时支持 Unicode,可以改变%n 参数顺序。
    1. //1.4 QString::arg()函数
    2. QString strtemp;
    3. strtemp = QString("%1 very nb and %2").arg("zgl").arg(666);
    4. qDebug()<
    5. qDebug()<<qPrintable(strtemp);
    2、常用查询字符串
    2.1 函数 QString::startswith()判断一个字符串是否以某个字符串开头。Qt::CaseInsensitive 代表大小写不敏感; Qt::CaseSensitive表示大小定敏感。对应关系函数:QString::endsWith()。
    1. //2.1 QString::startswith()函数
    2. QString strtemp = "zgl very nb";
    3. qDebug()<startsWith("zgl",Qt::CaseSensitive); //大小写敏感,返回值为true
    4. qDebug()<startsWith("Zgl",Qt::CaseSensitive); //大小写敏感,返回值为false
    5. qDebug()<startsWith("Zgl",Qt::CaseInsensitive); //大小写不敏感,返回值为true
    6. qDebug()<startsWith("nb",Qt::CaseInsensitive); //大小写不敏感,返回值为false
    2.2 函数 QString::contains (判断一个指定的字符串是否出现过)
    1. //2.2 QString::contains()函数
    2. QString strtemp = "zgl nb";
    3. qDebug()<contains("zgl",Qt::CaseSensitive); //返回值为true
    2.3 QString::toInt()函数将字符串转换为整型数值   toDouble () /toFloat () /toLong()等等
    1. //2.3 QString::toInt()函数
    2. QString str="25";
    3. bool isloop; //bool类型返回值为true或者false
    4. int hex = str.toInt(&isloop,16); //十六进制的25转换为十进制为37
    5. qDebug()<<"isloop="<","<<"hex="<
    2.4 QString::compare ( 函数对两个字符串进行比较)
    1. //2.4 QString::compare()函数
    2. int a1 = QString::compare("abc","Abc",Qt::CaseInsensitive); //大小写不敏感
    3. int b1 = QString::compare("abc","Abc",Qt::CaseSensitive); //大小写敏感
    4. int c1 = QString::compare("Abc","abc",Qt::CaseSensitive); //大小写敏感
    5. cout<<"a = "<","<<"b = "<","<<"c = "<//ASCII码大小写差32,A=65,a=97
    2.5 Qt将QString 转换成 ASCII码
    1. //2.5 Qt将QString 转换成 ASCII码 ASCII码大小写差32,A=65,a=97
    2. QString str = "ABC abc";
    3. QByteArray bytes = str.toUtf8();
    4. for(int i=0;isize();i++){
    5. qDebug()<<int(bytes.at(i));
    6. }

    二、Qt 常见基本数据类型(注意:定义在#include )

    QT基本数据类型定义在#include  中,QT基本数据类型有:

    类型名称注释备注
    qint8signed char有符号8位数据
    qint16signed short16位数据类型
    qint32signed short32位有符号数据类型
    qint64long long int 或(__int64)64位有符号数据类型,Windows中定义为__int64
    qintptrqint32 或 qint64指针类型 根据系统类型不同而不同,32位系统为qint32、64位系统为qint64
    qlonglonglong long int 或(__int64)Windows中定义为__int64
    qptrdiffqint32 或 qint64根据系统类型不同而不同,32位系统为qint32、64位系统为qint64
    qrealdouble 或 float除非配置了-qreal float选项,否则默认为double
    quint8unsigned char无符号8位数据类型
    quint16unsigned short无符号16位数据类型
    quint32unsigned int无符号32位数据类型
    quint64unsigned long long int 或 (unsigned __int64)无符号64比特数据类型,Windows中定义为unsigned __int64
    quintptrquint32 或 quint64根据系统类型不同而不同,32位系统为quint32、64位系统为quint64
    qulonglongunsigned long long int 或 (unsigned __int64)Windows中定义为__int64
    ucharunsigned char无符号字符类型
    uintunsigned int无符号整型
    ulongunsigned long无符号长整型
    ushortunsigned short无符号短整型

    QDateTime
    1. // 构造函数
    2. QDateTime::QDateTime();
    3. QDateTime::QDateTime(const QDate &date, const QTime &time, Qt::TimeSpec spec = Qt::LocalTime);
    4. ​
    5. // 公共成员函数
    6. // 设置日期
    7. void QDateTime::setDate(const QDate &date);
    8. // 设置时间
    9. void QDateTime::setTime(const QTime &time);
    10. // 给当前日期对象追加 年/月/日/秒/毫秒, 参数可以是负数
    11. QDateTime QDateTime::addYears(int nyears) const;
    12. QDateTime QDateTime::addMonths(int nmonths) const;
    13. QDateTime QDateTime::addDays(qint64 ndays) const;
    14. QDateTime QDateTime::addSecs(qint64 s) const;
    15. QDateTime QDateTime::addMSecs(qint64 msecs) const;
    16. ​
    17. // 得到对象中的日期
    18. QDate QDateTime::date() const;
    19. // 得到对象中的时间
    20. QTime QDateTime::time() const;
    21. ​
    22. // 日期和时间格式, 格式字符参考QDate 和 QTime 类的 toString() 函数
    23. QString QDateTime::toString(const QString &format) const;
    24. ​
    25. ​
    26. // 操作符重载 ==> 日期时间对象的比较
    27. bool QDateTime::operator!=(const QDateTime &other) const;
    28. bool QDateTime::operator<(const QDateTime &other) const;
    29. bool QDateTime::operator<=(const QDateTime &other) const;
    30. bool QDateTime::operator==(const QDateTime &other) const;
    31. bool QDateTime::operator>(const QDateTime &other) const;
    32. bool QDateTime::operator>=(const QDateTime &other) const;
    33. ​
    34. // 静态函数
    35. // 得到当前时区的日期和时间(本地设置的时区对应的日期和时间)
    36. [static] QDateTime QDateTime::currentDateTime();
    QByteArray
    1.     // QDateTime QByteArray
    2.     QDateTime dt;
    3.     QString strDT=dt.currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    4.     qDebug()<
    5.     QByteArray a1("Qt Creator Hello World.");
    6.     QByteArray b1=a1.toLower(); // 将字符串大写字母转小写,小写不变
    7.     qDebug()<
    8.     QByteArray c1=a1.toUpper();
    9.     qDebug()<

  • 相关阅读:
    C陷阱与缺陷 第7章 可移植性缺陷 7.1 应对C语言标准变更
    .NET服务发现(Microsoft.Extensions.ServiceDiscovery)集成Consul
    Java - 泛型
    SpringBoot SpringBoot 开发实用篇 4 数据层解决方案 4.11 SpringBoot 整合 MongoDB
    Mysql主从集群同步延迟问题怎么解决
    什么是圆锥的准线?
    webpack
    Burstormer论文阅读笔记
    回归与聚类算法系列④:岭回归
    通过API爬取到的淘宝商品详情数据展示(api测试入口)
  • 原文地址:https://blog.csdn.net/m0_74712453/article/details/133610037
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号