Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting the fourth line QString
Qt 6.11 is out! See what's new in the release blog

Getting the fourth line QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 997 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MrErfanM Offline
    MrErfanM Offline
    MrErfan
    wrote on last edited by
    #1

    hi ,
    I want to get the fourth line of a text inside QString

    QString str = "1 \n 2 \n 3 \n 4 \n 5 \n 6 \n";
    
    JonBJ 1 Reply Last reply
    0
    • A Offline
      A Offline
      arozon
      wrote on last edited by arozon
      #2

      Quite simply use

      foostring.split()...
      

      Split and pass the argument as a char of a linebreak.
      Then from the qstringlist the resulting indexes should be your line

      1 Reply Last reply
      3
      • MrErfanM MrErfan

        hi ,
        I want to get the fourth line of a text inside QString

        QString str = "1 \n 2 \n 3 \n 4 \n 5 \n 6 \n";
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @MrErfan
        QStringList list = str.split("\n");
        return list[3];

        Have a look at the overloads of http://doc.qt.io/qt-5/qstring.html#split to decide just what you want to split on, e.g. perhaps a QRegExp.

        EDIT: P.S. split() does not scale optimally if you have, say, a million+ lines in str --- let us know if that is a possible factor ;-)

        1 Reply Last reply
        5

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved