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. Get the string between two delimiters only.

Get the string between two delimiters only.

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 1.7k 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.
  • M Offline
    M Offline
    mbatra
    wrote on last edited by
    #1

    Hi,

    I have this sample string
    QString str = "$23,10000:SSIBRDBUNO,10001:AUG2101001,40010:374,40030:320,20050:100,20060:100,20070:200,20080:1,:,200A3:234,10002:01082021,10003:01082021,10010:0000001,10011:0000000,10022:0000001,10030:1,10031:01082021,10032:01082021,10060:1,10070:1@3237472528!";

    I want to get all the strings only between ':' and ',' delimiters. I have used the split function but I am also getting the characters in between ',' and ':'.

    Example output should be:
    SSIBRDBUNO
    AUG2101001
    374
    320
    100
    100
    etc.

    Any help would be appreciated.

    Thank you.

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      Hi @mbatra,

      I want to get all the strings only between ':' and ',' delimiters.

      Here's two options.

      The first uses QString::split() to split on the : and then trims everything after the ,:

          const auto parts = str.split(QLatin1Char(':')).mid(1);
          for (const auto &part: parts) {
              const QString item = part.left(part.indexOf(QLatin1Char(',')));
              qDebug() << item;
          }
      

      Option 2 uses regex to define the actual content you want to capture:

          const static QRegularExpression pattern(QStringLiteral(":([^,]*),"));
          for (auto iter = pattern.globalMatch(str); iter.hasNext(); ) {
              qDebug() << iter.next().captured(1);
          }
      

      They are both identical for your test string, both outputting:

      "SSIBRDBUNO"
      "AUG2101001"
      "374"
      "320"
      "100"
      "100"
      "200"
      "1"
      ""
      "234"
      "01082021"
      "01082021"
      "0000001"
      "0000000"
      "0000001"
      "1"
      "01082021"
      "01082021"
      "1"
      

      But they will differ on some input corner cases. Which one is "more correct" will depend on your specific use case/s.

      Cheers.

      M 1 Reply Last reply
      2
      • Paul ColbyP Paul Colby

        Hi @mbatra,

        I want to get all the strings only between ':' and ',' delimiters.

        Here's two options.

        The first uses QString::split() to split on the : and then trims everything after the ,:

            const auto parts = str.split(QLatin1Char(':')).mid(1);
            for (const auto &part: parts) {
                const QString item = part.left(part.indexOf(QLatin1Char(',')));
                qDebug() << item;
            }
        

        Option 2 uses regex to define the actual content you want to capture:

            const static QRegularExpression pattern(QStringLiteral(":([^,]*),"));
            for (auto iter = pattern.globalMatch(str); iter.hasNext(); ) {
                qDebug() << iter.next().captured(1);
            }
        

        They are both identical for your test string, both outputting:

        "SSIBRDBUNO"
        "AUG2101001"
        "374"
        "320"
        "100"
        "100"
        "200"
        "1"
        ""
        "234"
        "01082021"
        "01082021"
        "0000001"
        "0000000"
        "0000001"
        "1"
        "01082021"
        "01082021"
        "1"
        

        But they will differ on some input corner cases. Which one is "more correct" will depend on your specific use case/s.

        Cheers.

        M Offline
        M Offline
        mbatra
        wrote on last edited by
        #3

        @Paul-Colby

        Hi Paul,

        Thanks for your response. Its working fine with both the methods. Just one more help, the first method is fine for me:
        const auto parts = str.split(QLatin1Char(':')).mid(1);
        for (const auto &part: parts) {
        const QString item = part.left(part.indexOf(QLatin1Char(',')));
        qDebug() << item;
        }

        as I need the last string "1@3237472528!" also. But I need only 1 which is before '@' character. How to split the last string after ':' and before '@'.

        Thanks.

        Paul ColbyP 1 Reply Last reply
        0
        • M mbatra

          @Paul-Colby

          Hi Paul,

          Thanks for your response. Its working fine with both the methods. Just one more help, the first method is fine for me:
          const auto parts = str.split(QLatin1Char(':')).mid(1);
          for (const auto &part: parts) {
          const QString item = part.left(part.indexOf(QLatin1Char(',')));
          qDebug() << item;
          }

          as I need the last string "1@3237472528!" also. But I need only 1 which is before '@' character. How to split the last string after ':' and before '@'.

          Thanks.

          Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          as I need the last string "1@3237472528!"

          Oh right, the regex version didn't include that entry. To make them identical, the regex should have been:

              const static QRegularExpression pattern(QStringLiteral(":([^,]*)(,|$)"));
          

          But I need only 1 which is before '@' character. How to split the last string after ':' and before '@'.

          Well, it depends a bit on how definitive your example is... ie can random @ characters appear elsewhere in the string? It doesn't appear so (in your sample string), in which case either of these will do the trick:

          Split version:

              const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));
          

          Regex version:

              const static QRegularExpression pattern(QStringLiteral(":([^,]*)[,@]"));
          

          Again, which approach is better depends on the other possible variations of your input.

          Cheers.

          M 2 Replies Last reply
          2
          • Paul ColbyP Paul Colby

            as I need the last string "1@3237472528!"

            Oh right, the regex version didn't include that entry. To make them identical, the regex should have been:

                const static QRegularExpression pattern(QStringLiteral(":([^,]*)(,|$)"));
            

            But I need only 1 which is before '@' character. How to split the last string after ':' and before '@'.

            Well, it depends a bit on how definitive your example is... ie can random @ characters appear elsewhere in the string? It doesn't appear so (in your sample string), in which case either of these will do the trick:

            Split version:

                const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));
            

            Regex version:

                const static QRegularExpression pattern(QStringLiteral(":([^,]*)[,@]"));
            

            Again, which approach is better depends on the other possible variations of your input.

            Cheers.

            M Offline
            M Offline
            mbatra
            wrote on last edited by
            #5

            @Paul-Colby
            Hi Paul,

            Thank you very much for your response.

            @Paul-Colby said in Get the string between two delimiters only.:

            Well, it depends a bit on how definitive your example is... ie can random @ characters appear elsewhere in the string? It doesn't appear so (in your sample string), in which case either of these will do the trick:

            No, '@' will not appear elsewhere in the string. This worked fine.

            Thanks.

            1 Reply Last reply
            0
            • Paul ColbyP Paul Colby

              as I need the last string "1@3237472528!"

              Oh right, the regex version didn't include that entry. To make them identical, the regex should have been:

                  const static QRegularExpression pattern(QStringLiteral(":([^,]*)(,|$)"));
              

              But I need only 1 which is before '@' character. How to split the last string after ':' and before '@'.

              Well, it depends a bit on how definitive your example is... ie can random @ characters appear elsewhere in the string? It doesn't appear so (in your sample string), in which case either of these will do the trick:

              Split version:

                  const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));
              

              Regex version:

                  const static QRegularExpression pattern(QStringLiteral(":([^,]*)[,@]"));
              

              Again, which approach is better depends on the other possible variations of your input.

              Cheers.

              M Offline
              M Offline
              mbatra
              wrote on last edited by
              #6

              @Paul-Colby

              Hi Paul,

              Its working fine. I am facing one issue. Earlier I had created a QString and saved it in csv file as below:

              QFile file("D:\data.csv");
              if(file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ))
              {
              QTextStream out(&file);
              out << query7; // query7 is a QString.
              }

              But when I tried to insert item into csv file it doesn't store in csv format, it stores the entire string into one column.

              QFile file("D:\\v1.7\\data.csv");
              file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text );
              
              const auto parts = str.split(QLatin1Char(':')).mid(1);
              for (const auto &part: parts) {
                  //const QString item = part.left(part.indexOf(QLatin1Char(',')));
                  const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));
                  qDebug() << "item :"<<item;
              
                  QTextStream out(&file);
                  out << item;
              }
              

              Can you help me to resolve this issue.

              Thanks.

              jsulmJ 2 Replies Last reply
              0
              • M mbatra

                @Paul-Colby

                Hi Paul,

                Its working fine. I am facing one issue. Earlier I had created a QString and saved it in csv file as below:

                QFile file("D:\data.csv");
                if(file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ))
                {
                QTextStream out(&file);
                out << query7; // query7 is a QString.
                }

                But when I tried to insert item into csv file it doesn't store in csv format, it stores the entire string into one column.

                QFile file("D:\\v1.7\\data.csv");
                file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text );
                
                const auto parts = str.split(QLatin1Char(':')).mid(1);
                for (const auto &part: parts) {
                    //const QString item = part.left(part.indexOf(QLatin1Char(',')));
                    const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));
                    qDebug() << "item :"<<item;
                
                    QTextStream out(&file);
                    out << item;
                }
                

                Can you help me to resolve this issue.

                Thanks.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @mbatra said in Get the string between two delimiters only.:

                one column

                One column or one line? If one line simply write a new line into the file after each line.
                Also, why do you create QTextStream instance inside the loop? This is wasting CPU cycles for nothing.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @mbatra said in Get the string between two delimiters only.:

                  one column

                  One column or one line? If one line simply write a new line into the file after each line.
                  Also, why do you create QTextStream instance inside the loop? This is wasting CPU cycles for nothing.

                  M Offline
                  M Offline
                  mbatra
                  wrote on last edited by
                  #8

                  @jsulm

                  Hi,

                  One String in each column. I have moved QTextStream outside the loop for now.
                  But All strings are comin in one column only.

                  jsulmJ 1 Reply Last reply
                  0
                  • M mbatra

                    @jsulm

                    Hi,

                    One String in each column. I have moved QTextStream outside the loop for now.
                    But All strings are comin in one column only.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @mbatra Can you show how it should look like and what you really get?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 2 Replies Last reply
                    0
                    • jsulmJ jsulm

                      @mbatra Can you show how it should look like and what you really get?

                      M Offline
                      M Offline
                      mbatra
                      wrote on last edited by
                      #10

                      @jsulm

                      f5b4617e-3079-4660-a32a-32e80913b031-image.png

                      I am getting like this.
                      Each string should be stored in different columns.

                      JonBJ 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @mbatra Can you show how it should look like and what you really get?

                        M Offline
                        M Offline
                        mbatra
                        wrote on last edited by
                        #11

                        @jsulm

                        Instead, it should come like this
                        2bb60072-b788-4c92-a1cd-13b52ed621a9-image.png

                        1 Reply Last reply
                        0
                        • M mbatra

                          @jsulm

                          f5b4617e-3079-4660-a32a-32e80913b031-image.png

                          I am getting like this.
                          Each string should be stored in different columns.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #12

                          @mbatra
                          Look in the .csv file, not what Excel or whatever spreadsheet you are using shows you. How are the columns separated in the file? Did you output space, comma or tab or whatever you want/need to separate the strings into columns? "CSV" stands for "comma separated values". In English you need to put commas between the columns, but I have seen in other posts that if your language uses , in floating point numbers for English decimal point (.) then I believe you are supposed to use some other separator.

                          1 Reply Last reply
                          1
                          • M mbatra

                            @Paul-Colby

                            Hi Paul,

                            Its working fine. I am facing one issue. Earlier I had created a QString and saved it in csv file as below:

                            QFile file("D:\data.csv");
                            if(file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ))
                            {
                            QTextStream out(&file);
                            out << query7; // query7 is a QString.
                            }

                            But when I tried to insert item into csv file it doesn't store in csv format, it stores the entire string into one column.

                            QFile file("D:\\v1.7\\data.csv");
                            file.open(QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text );
                            
                            const auto parts = str.split(QLatin1Char(':')).mid(1);
                            for (const auto &part: parts) {
                                //const QString item = part.left(part.indexOf(QLatin1Char(',')));
                                const QString item = part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));
                                qDebug() << "item :"<<item;
                            
                                QTextStream out(&file);
                                out << item;
                            }
                            

                            Can you help me to resolve this issue.

                            Thanks.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @mbatra said in Get the string between two delimiters only.:

                            part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));

                            I'm not sure this is correct: you're calling left() and then on the result of that call you again call left but use result from indexOf() from the whole string not the part returned from first left() call.
                            Does qDebug() << "item :"<<item; output what you want?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            M 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @mbatra said in Get the string between two delimiters only.:

                              part.left(part.indexOf(QLatin1Char(','))).left(part.indexOf(QLatin1Char('@')));

                              I'm not sure this is correct: you're calling left() and then on the result of that call you again call left but use result from indexOf() from the whole string not the part returned from first left() call.
                              Does qDebug() << "item :"<<item; output what you want?

                              M Offline
                              M Offline
                              mbatra
                              wrote on last edited by
                              #14

                              @jsulm

                              Yes, it outputs as expected. All the strings.
                              But when I write the strings into .csv file, it enters aal the string into one column.

                              jsulmJ 1 Reply Last reply
                              0
                              • M mbatra

                                @jsulm

                                Yes, it outputs as expected. All the strings.
                                But when I write the strings into .csv file, it enters aal the string into one column.

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @mbatra Then please do what @JonB suggested: open the file in a text editor. Do you actually write any separator like ',' to separate the columns?

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                JonBJ 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @mbatra Then please do what @JonB suggested: open the file in a text editor. Do you actually write any separator like ',' to separate the columns?

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @jsulm said in Get the string between two delimiters only.:

                                  Do you actually write any separator like ',' to separate the columns?

                                  The code indicates that the OP does not....

                                  M 1 Reply Last reply
                                  0
                                  • JonBJ JonB

                                    @jsulm said in Get the string between two delimiters only.:

                                    Do you actually write any separator like ',' to separate the columns?

                                    The code indicates that the OP does not....

                                    M Offline
                                    M Offline
                                    mbatra
                                    wrote on last edited by
                                    #17

                                    @JonB

                                    Hi,

                                    I have added the separator. Its working fine.

                                    Thank you everyone for your support.

                                    1 Reply Last reply
                                    0
                                    • M mbatra has marked this topic as solved on

                                    • Login

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