zoukankan      html  css  js  c++  java
  • 字符串类QString

    采用Unicode编码,所以一个QChar占用两个字节
    使用隐式共享技术来节省内存和减少不必要的数据拷贝
    跨平台使用,不用考虑字符串的平台兼容性
    QString直接支持字符串和数字之间的相互转换
    QString直接支持字符串之间的大小比较(按照字典序)
    QString直接支持不同编码下的字符串转换
    QString直接支持std::string和std::wstring之间的相互转换
    QString直接支持正则表达式的使用

    QString对象构造:

    QChar qch[5]={'a','b','c','d','e'};
        QString str=QString (qch , 3 );  //使用QChar数组构造QString
        //参数2:取前3个字符组成字符串
        str=QString (qch );
        //qch的所有字符组成字符串
        QChar* p=qch;
        str=QString (p);  //使用QChar数组指针构造QString
        str="中国";

    追加字符串:

    QString s = "str";
        s += "ing";//字符串相加,s = "string"
        s.append("|");  // 尾部添加
        s.push_back("lm");  // 尾部添加
        s.prepend("This is ");//前面追加
        s.push_front("中国");  //前面追加

    格式输出: 

    QString s;
        s.sprintf("%s %.1f%%", "Value", 100.0);//格式输出方式一---s = "Value 100.0%"
        //符号的意思参看:https://www.cnblogs.com/liming19680104/p/11216495.html  
        s = QString("%1 %2 %3 %4").arg("This").arg("is").arg("a").arg("test");//格式输出方式二---s = "This is a test"

    插入及替换:

    QString s="我是中国人";
        s.insert(2,"优秀的");  //插入字符串
        //参数1:插入位置---从0开始
        //参数2:要插入的字符串
    
        s.replace(5,2,"呼和浩特");  //替换
        //参数1:开始替换的位置---从0开始
        //参数2:要被替换掉的字符数
        //参数3:用来替换的字符串

    查找:

    QString s="我ef是ab中国cd人Abef";
        int i=s.indexOf("Ab",0,Qt::CaseInsensitive);  //查找,返回找到的位置序号
           //参数1:待查找的字符串,也可以是正则表达式
           //参数2:开始查找的位置;如果-1表示从最后一个字符开始,如果-2表示从倒数第二个开始,以此类推
           //Qt::CaseSensitive   区分大小写--默认;Qt::CaseInsensitive   不区分大小写
           //返回待查找字符串第一次出现的位置,没有找到目标字符串返回-1
        qDebug()<<i;
        i=s.lastIndexOf("Ab");  //从尾部开始查找

    清除子串: 

    QString s="我ef是ab中国cd人Abef";
        QString str;
        str=s.remove("Abc",Qt::CaseInsensitive);  //清除所有子串
        //参数1:待清除的字符串
        //Qt::CaseSensitive   区分大小写--默认;Qt::CaseInsensitive   不区分大小写
        //返回清除后的子串;找不到子串返回原字符串

    提取字符串:

    QString s = "QString";
        QString ss = s.mid(1,3); //提取字符串
        //参数1:开始提取的位置
        //参数2:要提取的字符个数;可以省略:提取到末尾
        //ss = "Str"
        ss = s.left(4);//提取左边的n个字符
        //ss = "QStr"
        ss=s.right(3);  //提取右边的n个字符
        //ss = "ing"

    字符串比较: 

    QString str="123";
        QString str1="789";
        bool bl=str != str1; //两个字符串是否不等于
        bl=str == str1; //两个字符串是否等于
        bl=str < str1; //是否小于
        bl=str > str1; //是否大于
        bl=str <= str1; //是否小于等于
        bl=str >= str1; //是否大于等于

    判断字符串是否存在:

    QString str="abcEfg";
        bool bl=str.contains("ce",Qt::CaseInsensitive);  //是否包含子串
        ////Qt::CaseSensitive   区分大小写--默认;Qt::CaseInsensitive   不区分大小写

    判断字符串是否是empty:

    bool    isEmpty () const//原型
    
    QString().isEmpty();            // returns true
    QString("").isEmpty();          // returns true
    QString("x").isEmpty();         // returns false
    QString("abc").isEmpty();       // returns false

    判断字符串是否NULL"

    bool    isNull () const//原型
    
    QString().isNull();             // returns true
    QString("").isNull();           // returns false
    QString("abc").isNull();        // returns false

    分隔字符串:

    通过使用QString::split()能够将一个大串按照某个子串划分成多个子串并存到QStringList中

    QString str = "You,I,She";
        QStringList list= str.split(",");  //分隔字符串
        //参数:分割字符串
        //("You", "I", "She")

    section()

    section() 函数的原型为:

    QString section (const QString &sep, int start, int end = -1, SectionFlags flags = SectionDefault) const

    其功能是从字符串中提取以 sep 作为分隔符,从 start 端到 end 端的字符串

    QString str2, str1="学生姓名,男,1984-3-4,汉族,山东";
    str2=str1.section (",",0,0); // str2="学生姓名", 第 1 段的编号为 0
    str2=str1.section (",",1,1}; // str2="男"
    str2=str1.section (",",0,1}; // str2="学生姓名,男"
    str2=str1.section (",",4,4); // str2="山东"

    去除首尾空格: 

    QString s("   abc def ghi   ");
        s = s.trimmed();//去除首尾空格

    simplified() 不仅去掉首尾的空格,中间连续的空格也用一个空格替换

    大小写转换:

    QString s = "Hello World";
        QString ss = s.toUpper();  //都转换成大写
        ss = s.toLower();  //都转换成小写

    判断是否以某个字符串开始或结束:

    QString s = "http:www.baidu.com";
        bool bl = s.startsWith("http:");  //是否以某个字符串开始
        bl = s.endsWith("com");  //是否以某个字符串结束

    获取子串出现的次数:

    QString s = "abc  Abcefg nbgabc";
        int i=s.count("abc");//获取子串出现的次数
        //参数2:Qt::CaseSensitive   区分大小写--默认;Qt::CaseInsensitive   不区分大小写
    
    

    获取字符串长度:

    QString s = "我是中国人";
        int i=s.length();//获取字符串长度--字符数

    也可以用    i=s.size();

  • 相关阅读:
    PyCharm设置改变字体大小的快捷键
    python中的字符串
    python入门知识
    css3(border-radius)边框圆角详解
    js中__proto__和prototype的区别和关系?
    常见的浏览器兼容问题
    Meta http-equiv属性详解(转)
    WinForm界面设计-Button添加背景图去边框
    vs2015 c# winfrom应用程序打包成64位
    vsto-Word相关操作
  • 原文地址:https://www.cnblogs.com/liming19680104/p/11736098.html
Copyright © 2011-2022 走看看