zoukankan      html  css  js  c++  java
  • Qt入门-字符串类QString

    原地址:http://blog.csdn.net/xgbing/article/details/7770854

    QString是Unicode字符的集合,它是Qt API中使用的字符串类。

        QString的成员是QChar,QChar是一个16位Unicode字符类。大多数编译器把它看作是一个unsigned short。

        QString和C标准中的字符串不同,它不以''结尾,相反,QString可以嵌入''/字符。

        

    (1)QString初始化。

    1. QString str("Hello");  
    2.   
    3. QString str = "Hello";  
    4.   
    5. static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };  
    6. QString str(data, 4);  
    7.   
    8. QString str;  
    9. str.resize(4);  
    10.   
    11. str[0] = QChar('U');  
    12. str[1] = QChar('n');  
    13. str[2] = QChar(0x10e3);  
    14. str[3] = QChar(0x03a3);  
    15.   
    16. QString str;  
    17. str.sprintf("%s %.3f""float", 3.1415926);  
    18. //str结果是"float 3.14"  
    19.   
    20. QString str;  
    21. str.setNum(10);  //str = "10"  
    22. str.setNum(10, 16); //str = "a"  
    23. str.setNum(10.12345); //str = "10.12345"  
    24.   
    25. QString i;           // current file's number  
    26. QString total;       // number of files to process  
    27. QString fileName;    // current file's name  
    28.   
    29. QString status = QString("Processing file %1 of %2: %3")  
    30.                      .arg(i).arg(total).arg(fileName);  


    (2)字符串转换

    1. long    toLong ( bool * ok = 0, int base = 10 ) const  
    2. qlonglong   toLongLong ( bool * ok = 0, int base = 10 ) const  
    3. short   toShort ( bool * ok = 0, int base = 10 ) const  
    4. double  toDouble ( bool * ok = 0 ) const  
    5. float   toFloat ( bool * ok = 0 ) const  
    6. uint    toUInt ( bool * ok = 0, int base = 10 ) const  
    7. ulong   toULong ( bool * ok = 0, int base = 10 ) const  
    8. qulonglong  toULongLong ( bool * ok = 0, int base = 10 ) const  
    9. ushort  toUShort ( bool * ok = 0, int base = 10 ) const  

    参数ok结果说明转换是否成功。
    示例:

    1. QString str;  
    2. bool ok;    
    3. double d = str.toDouble(&ok);    
    4. if(ok)    
    5. {    
    6.     cout << str << endl;    
    7. else  
    8. {  
    9.     cout << "error." << endl;  
    10. }  



    (3)字符串比较

    1. int compare ( const QString & other ) const  
    2. int compare ( const QString & other, Qt::CaseSensitivity cs ) const  
    3. int compare ( const QLatin1String & other, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const  
    4. int compare ( const QStringRef & ref, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const  
    1. QString().isNull();               // returns true  
    2. QString().isEmpty();              // returns true  
    3.   
    4. QString("").isNull();             // returns false  
    5. QString("").isEmpty();            // returns true  
    6.   
    7. QString("abc").isNull();          // returns false  
    8. QString("abc").isEmpty();         // returns false  


    (4)字符串处理

    QStringleft ( int n ) const  //取左边的n个字符。
    QStringright ( int n ) const //取右边的n个字符。

    replace()函数提供方法替换字符串。

    remove()函数从字符串中移除字符。

    split()函数拆分字符串。

    mid()取子串。

  • 相关阅读:
    Mysql使用存储过程快速添加百万数据
    ArcMap连接远程数据库报错“ Connection was attempted with an older version of SQL Server client communications software that is not supported.”
    uart write example
    Solution to errors like /lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found
    读.txt数据并合并~读npy数据并合并
    JVM类加载路径查看
    磨人的R语言安装问题(以安装monocle为例)
    js 删除数组几种方法
    vue 脚手架运行报错常见问题--'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序 或批处理文件
    vue 脚手架运行报错常见问题--vue-cli Failed to download repo vuejs-templates/webpack
  • 原文地址:https://www.cnblogs.com/lanye/p/3600427.html
Copyright © 2011-2022 走看看