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.
Forum Updated to NodeBB v4.3 + New Features

Get the string between two delimiters only.

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 2.2k Views 1 Watching
  • 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 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