QString 截取分割字符串


 

QStringList QString::split ( const QChar & sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const

 

  

https://blog.csdn.net/xuleisdjn/article/details/51438162?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-4

Title :

  • QString
  • QString::section()
  • QString::split()

Q :

如何從一段由特殊符號分隔的 QString 中獲取被分隔的子串?

  • 從字符串“one, two, three, four”中獲取第二個由‘,’分隔的子串,即“two” ;
  • QString str = "one, two, three, four";
    cout << str.section(',', 1, 1).trimmed().toStdString() << endl;
    

    結果是 “two”,前后不包含空格。上面的函數 trimmed() 是去掉字符串前后的ASCII字符 ‘\t’, ‘\n’, ‘\v’, ‘\f’, ‘\r’, and ’ ‘,這些字符用QChar::isSpace()判斷都返回true。重點是 section()函數,見下面詳細。

    • 從字符串 “one, two* three / four / five ^ six”中獲取第四個由 ‘,’、‘*’、 ‘/’和’^’分隔的子串,即“four”;

      

    QString str = "one, two* three / four / five ^ six";
    cout << str.section(QRegExp("[,*/^]"), 3, 3).trimmed().toStdString() << endl;
    

     上面用到了一個簡單的正則表達式,在Qt中可以由類QRegExp構造,函數 section() 支持使用正則表達式。

  • 重點內容

    Fn 1 :

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

    這個函數把字符串看成是幾個塊,這些塊由 sep 分隔,start 和 end 指定塊號,end 默認為 –1 ,返回的是[ start, end ]內的塊組成的字符串,如果 start 和 end 都是負數,那么將從字符串的后面往前面數,返回 [ -end, –start ]內的塊組成的字符串。SectionFlags是一些標記,如SectionSkipEmpty表示如果兩個分隔符之間是空串,那么就會跳過。下面的代碼是摘自QtDoc 4.7:  

  • 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"
    
    str = csv.section(',', -3, -2);  // str == "middlename,surname"
    str = path.section('/', -1); // str == "myapp"
    

    另外這個函數的另兩個重載函數如下: 

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

    第二個支持正則表達式作為分隔符判定,在某些場合下是不可替代的,如上面的問題中的第二個。

    Fn 2 :

  •  

    QStringList QString::split ( const QChar & sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
    

    這個函數把所有的由 sep 分隔的塊裝進一個 QStringList 中返回, 這個函數同樣有兩個重載:

  • QStringList QString::split ( const QString & sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
    QStringList QString::split ( const QRegExp & rx, SplitBehavior behavior = KeepEmptyParts ) const
    

    使用 split() 函數對上述兩個問題中的第二個進行求解:

  • QString str = "one, two* three / four / five ^ six";
    QStringList sections = str.split(QRegExp("[,*/^]")); //把每一個塊裝進一個QStringList中
    cout << sections.at(3).trimmed().toStdString() << endl;
    

      

      

      


免責聲明!

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



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