• 字符串


     QString类、QByteArray类

    字符串组合:

     1 #include "dialog.h"  //用户自定义控件
     2 #include <QApplication>  //这个头文件是应用程序必须的
     3 #include <QDebug>
     4 
     5 int main(int argc, char *argv[])
     6 {
     7     QApplication a(argc, argv);
     8 
     9     /*<字符串组合: + += >*/
    10     QString str1 = "welcome ";
    11     str1 + "to you! "; //QString类重载了 = 运算符
    12     /*
    13     inline QT_ASCII_CAST_WARN const QString operator+(const QString &s1, const char *s2)
    14     当然,还有其他重载:比如
    15     inline QT_ASCII_CAST_WARN const QString operator+(const char *s1, const QString &s2)
    16     */
    17 
    18     qDebug()<< (str1 + "to you! ");  //输出结果,调试语句,头文件:<QDebug>
    19 
    20     QString str2 = "Hello, ";
    21     str2 += "world! ";    //QString类重载了 += 运算符
    22     qDebug()<< str2;
    23 
    24     /*<字符串组合:append()方法相当于 +=的作用 >*/
    25     QString str3 = str1.append("to you! ");
    26     qDebug()<< str3;
    27 
    28     /*<字符串组合:sprintf() >*/
    29     QString str4;
    30     str4.sprintf("%s",str1.data());  //str4 = "welcome "  inline const QChar *QString::data() const,返回类型 const char*字符串
    31     str4.sprintf("%s %s",str1.data(),"to you! "); //str4 = "welcome to you! "
    32     qDebug()<< str4;
    33 
    34     /*<字符串组合:QString::arg() >*/
    35     QString str5;
    36     str5 = QString("%1 was born in %2.").arg("John").arg(1998);
    37     qDebug()<< str5;
    38 
    39     /*
    40     insert() 字符串特定位置插入另一字符
    41     prepend() 字符串开头插入另一个字符
    42     replace() 替换字符
    43     trimmed() 移除字符串两端的空格字符
    44     */
    45     return a.exec();
    46 }

    字符串查找:

    字符串转换:

     1 #include "dialog.h"  //用户自定义控件
     2 #include <QApplication>  //这个头文件是应用程序必须的
     3 #include <QDebug>
     4 
     5 int main(int argc, char *argv[])
     6 {
     7     QApplication a(argc, argv);
     8 
     9     /*字符串转换*/
    10     QString str = "0x6";
    11     bool ok;
    12 
    13     /*
    14     int toInt(bool *ok=nullptr, int base=10) const;
    15     第一个参数,返回函数的执行结果,是否转换成功。
    16     第二个参数,表示要转换的进制数,默认是10进制
    17     */
    18     int hex = str.toInt(&ok,16);
    19     qDebug()<< hex;
    20 
    21     /*将QString 转换为QByteArray */
    22     /*
    23     QByteArray具有一个字节数组,可以存放原始字节
    24     可以存放以‘’结束的字符串
    25     */
    26     QString str1 = "Welcom to you! ";
    27     /*
    28     toAscii()方法,可以将unicode编码的QString字符集转为ASCII编码方式的QByteArray字符集。
    29     */
    30     QByteArray ba = str1.toAscii();
    31     qDebug()<<ba;
    32 
    33     /*
    34      * NULL字符串与empty字符串
    35      * NULL字符串,使用构造函数: func((const char*)var = 0);
    36      * empty字符串是字符串大小为0,
    37      */
    38     if(QString().isNull() == true){
    39          qDebug()<< "is null? true";
    40     }else if(QString().isEmpty() == true){
    41        qDebug()<< "is empty? true";
    42     }
    43 
    44     if(QString("").isNull() == true){
    45         qDebug()<< "is null? true";
    46     }else if(QString("").isEmpty() == true){
    47         qDebug()<< "is empty? true";
    48     }
    49 
    50     return a.exec();
    51 }
    内在的趣味,表面的繁琐
  • 相关阅读:
    【流量劫持】SSLStrip 终极版 —— location 瞒天过海
    【流量劫持】沉默中的狂怒 —— Cookie 大喷发
    【流量劫持】SSLStrip 的未来 —— HTTPS 前端劫持
    Web 前端攻防(2014版)
    流量劫持 —— 浮层登录框的隐患
    流量劫持能有多大危害?
    流量劫持是如何产生的?
    XSS 前端防火墙 —— 整装待发
    XSS 前端防火墙 —— 天衣无缝的防护
    XSS 前端防火墙 —— 无懈可击的钩子
  • 原文地址:https://www.cnblogs.com/data1213/p/10741431.html
Copyright © 2020-2023  润新知