QString組合、拆分。


1、組合字符常用arg()函數

QString  test=QString("_haha_%1_hehe%2") .arg("ee").arg("aa");      //test="_haha_ee_heheaa"

eg:arg(const QString &a, int fieldWidth = 0, QChar fillChar = QLatin1Char( '  ' )) const        參數1.連接的字符;參數2.字符所占據的寬度;參數3.如果字符的寬度小於參數2的寬度,則用參數3的字符填充。

如下圖所示:fieldWidth >0 ,代表的右對齊;   fieldWidth <0,代表的左對齊。如下圖所示:

  

.

2、提取具有分割符的字符串用section()函數

QString QString::section(QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault) const    //參數1,代表分隔符;參數2,提取字符的起始位置;參數3,提取字符的結束位置。

eg: 

QString str;

QString csv = "forename,middlename,surname,phone";

QString path = "/usr/local/bin/myapp"; // First field is empty

QString::SectionFlag flag = QString::SectionSkipEmpty;    //Treat empty fields as if they don't exist, i.e. they are not considered as far as start and end are concerned.

str = csv.section(',', 2, 2); // str == "surname"。("forename,middlename,surname,phone")

str = path.section('/', 3, 4); // str == "bin/myapp"。("/usr/local/bin/myapp")

str = path.section('/', 3, 3, flag); // str == "myapp"  ("/usr/local/bin/myapp")空字符串開始的地方不被當做開始或結束的地方

3、QString在結構體中是直接當做4個字節的指針來占用內存的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM