zoukankan      html  css  js  c++  java
  • Qt 的QString类的使用

    Qt的QString类提供了很方便的对字符串操作的接口。

    1. 使某个字符填满字符串,也就是说字符串里的所有字符都有等长度的ch来代替。
    QString::fill ( QChar ch, int size = -1 )

    例:

         QString str = "Berlin";
         str.fill('z');
         // str == "zzzzzz"
    
         str.fill('A', 2);
         // str == "AA"

    2,从字符串里查找相同的某个字符串str。

    int QString::indexOf ( const QString & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const

    例如:

    QString x = "sticky question";
         QString y = "sti";
         x.indexOf(y);               // returns 0
         x.indexOf(y, 1);            // returns 10
         x.indexOf(y, 10);           // returns 10
         x.indexOf(y, 11);           // returns -1

    3指定位置插入字符串

    QString & QString::insert ( int position, const QString & str )

    例如:

         QString str = "Meal";
         str.insert(1, QString("ontr"));
         // str == "Montreal"

    3,判断字符串是否为空。

    bool QString::isEmpty () const

    如:

         QString().isEmpty();            // returns true
         QString("").isEmpty();          // returns true
         QString("x").isEmpty();         // returns false
         QString("abc").isEmpty();       // returns false

    4.判断字符串是否存在。

    bool QString::isNull () const

    例如:

         QString().isNull();             // returns true
         QString("").isNull();           // returns false
         QString("abc").isNull();        // returns false

    5,从左向右截取字符串

    QString QString::left ( int n ) const

    例如:

         QString x = "Pineapple";
         QString y = x.left(4);      // y == "Pine"

    6,从中间截取字符串。

    QString QString::mid ( int position, int n = -1 ) const

    例如:

      QString x = "Nine pineapples";
         QString y = x.mid(5, 4);            // y == "pine"
         QString z = x.mid(5);               // z == "pineapples"

    7,删除字符串中间某个字符。

    QString & QString::remove ( int position, int n )

    例如:

         QString s = "Montreal";
         s.remove(1, 4);
         // s == "Meal"

    8,替换字符串中的某些字符。

    QString & QString::replace ( int position, int n, const QString & after )

    例如:

         QString x = "Say yes!";
         QString y = "no";
         x.replace(4, 3, y);
         // x == "Say no!"

    9,以某个字符切割字符串。(最近经常用到的)

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

    例如:

    复制代码
       QString str;
         QString csv = "forename,middlename,surname,phone";
         QString path = "/usr/local/bin/myapp"; // First field is empty
         QString::SectionFlag flag = QString::SectionSkipEmpty;
    
         str = csv.section(',', 2, 2);   // str == "surname"
         str = path.section('/', 3, 4);  // str == "bin/myapp"
         str = path.section('/', 3, 3, flag); // str == "myapp"
    复制代码

    10,把整型,浮点型,或其他类型转化为QString

    QString & QString::setNum ( uint n, int base = 10 )

    相类似的还有好多重载函数,想深入了解,还是要看Qt帮助文档的。

    转http://www.cnblogs.com/onlycxue/archive/2012/10/30/2746902.html

  • 相关阅读:
    leetcode Power of Two
    leetcode Merge Two Sorted Lists
    Mac linux 安装memcached服务 用法
    Vsftp配置都没有问题 连接不上 530 Login incorrect 解决方法
    mysql 远程连接不上 %用户已经添加了
    Centos yum 安装mysql报错 No package mysql-server available.
    Linux 查询程序安装路径 是否安装
    php报错 Call to undefined function mb_stripos()
    mac编译openssl扩展报错 openssl.c:44:10: fatal error: 'openssl/evp.h' file not found
    在安装mysqli的时候,出现error: ext/mysqlnd/mysql_float_to_double.h: No such file or direc
  • 原文地址:https://www.cnblogs.com/Bonker/p/3625679.html
Copyright © 2011-2022 走看看