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. How to remove quotes and eol characters from QString?
Forum Update on Monday, May 27th 2025

How to remove quotes and eol characters from QString?

Scheduled Pinned Locked Moved Solved General and Desktop
qstringquotescarriage returnnew line
9 Posts 3 Posters 4.6k 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.
  • E Offline
    E Offline
    emp1953
    wrote on 13 Jan 2020, 01:49 last edited by
    #1

    I have the following code:

    QString pathname = QDir::currentPath();
        QString fpath = pathname + fname;
        QFile fs(fpath);
    
        int counter;
        if(!fs.open(QIODevice::ReadOnly))
        {
            QMessageBox::information(0,"file info ",fs.errorString());
        }else{
            QString line = fs.readLine(); // in = your QTextStream
                ui->lw_lot_nbr_fd1->addItem(line);
            while (!line.isNull())
            {
                line = fs.readLine();
                //tempstr =  QString::split('"');
                if(line.startsWith("""))
                {
                    line.remove(0,1);
                }
                if(line.endsWith("""))
                {
                    line.remove(line.size()-1,1);
                }
    
    

    The line.startsWith("""); does not evaluate to true even though there is a quote there.
    the line.endsWith("""); does not evaluate to true even though there is a quote there.
    If I change to line.endsWith("\r") or line.endsWith("\n") ; does not evaluate to true even though there is a return and new line there.

    The strings are read from a text file. Each line is a number as:
    00001
    00002
    00003
    00004 etc.

    When debugging and I hover over the variable "line" it says "00001\r\n

    When displayed in my QListWidget the leading quote is there as is a tiny little square

    A hexdump of the text file gives:
    0000000 3030 3030 0d31 300a 3030 3230 0a0d 3030
    0000010 3030 0d33 300a 3030 3430 0a0d 3030 3030
    0000020 0d35 300a 3030 3630 0a0d 3030 3030 0d37
    0000030 300a 3030 3830 0a0d 3030 3030 0d39 300a
    0000040 3030 3031 0a0d 3030 3130 0d31 300a 3030
    0000050 3231 0a0d 3030 3130 0d33 300a 3030 3431
    0000060 0a0d 3030 3130 0d35 300a 3030 3631 0a0d
    0000070 3030 3130 0d37 300a 3030 3831 0a0d 3030
    0000080 3130 0d39 300a 3030 3032 0a0d 3030 3230
    0000090 0d31 300a 3030 3232 0a0d 3030 3230 0d33
    00000a0 300a 3030 3432 0a0d 3030 3230 0d35 300a

    The text file originally came from a windows system, is this part of the problem.

    Thanks for any help

    emp1953

    E A 3 Replies Last reply 13 Jan 2020, 02:25
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 13 Jan 2020, 05:54 last edited by
      #4

      I would go with QString::trimmed()

      Your hexdump doesn't look like a windows text file and does not match your expected strings

      3030 3030 0d31 300a 3030 3230 0a0d 3030
      0 0 0 0 <CR> 1 0 <LF> 0 0 2 0 <CR><LF> 0 0

      This does not look correct...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      E 1 Reply Last reply 14 Jan 2020, 21:16
      4
      • E emp1953
        13 Jan 2020, 01:49

        I have the following code:

        QString pathname = QDir::currentPath();
            QString fpath = pathname + fname;
            QFile fs(fpath);
        
            int counter;
            if(!fs.open(QIODevice::ReadOnly))
            {
                QMessageBox::information(0,"file info ",fs.errorString());
            }else{
                QString line = fs.readLine(); // in = your QTextStream
                    ui->lw_lot_nbr_fd1->addItem(line);
                while (!line.isNull())
                {
                    line = fs.readLine();
                    //tempstr =  QString::split('"');
                    if(line.startsWith("""))
                    {
                        line.remove(0,1);
                    }
                    if(line.endsWith("""))
                    {
                        line.remove(line.size()-1,1);
                    }
        
        

        The line.startsWith("""); does not evaluate to true even though there is a quote there.
        the line.endsWith("""); does not evaluate to true even though there is a quote there.
        If I change to line.endsWith("\r") or line.endsWith("\n") ; does not evaluate to true even though there is a return and new line there.

        The strings are read from a text file. Each line is a number as:
        00001
        00002
        00003
        00004 etc.

        When debugging and I hover over the variable "line" it says "00001\r\n

        When displayed in my QListWidget the leading quote is there as is a tiny little square

        A hexdump of the text file gives:
        0000000 3030 3030 0d31 300a 3030 3230 0a0d 3030
        0000010 3030 0d33 300a 3030 3430 0a0d 3030 3030
        0000020 0d35 300a 3030 3630 0a0d 3030 3030 0d37
        0000030 300a 3030 3830 0a0d 3030 3030 0d39 300a
        0000040 3030 3031 0a0d 3030 3130 0d31 300a 3030
        0000050 3231 0a0d 3030 3130 0d33 300a 3030 3431
        0000060 0a0d 3030 3130 0d35 300a 3030 3631 0a0d
        0000070 3030 3130 0d37 300a 3030 3831 0a0d 3030
        0000080 3130 0d39 300a 3030 3032 0a0d 3030 3230
        0000090 0d31 300a 3030 3232 0a0d 3030 3230 0d33
        00000a0 300a 3030 3432 0a0d 3030 3230 0d35 300a

        The text file originally came from a windows system, is this part of the problem.

        Thanks for any help

        emp1953

        E Offline
        E Offline
        emp1953
        wrote on 13 Jan 2020, 02:25 last edited by
        #2

        @emp1953 It would help to tell you that I am using Qt 4.8, Fedora 19 The tiny little square I refer to above shows up after every item in the QListWidget.

        Thanks

        1 Reply Last reply
        0
        • E emp1953
          13 Jan 2020, 01:49

          I have the following code:

          QString pathname = QDir::currentPath();
              QString fpath = pathname + fname;
              QFile fs(fpath);
          
              int counter;
              if(!fs.open(QIODevice::ReadOnly))
              {
                  QMessageBox::information(0,"file info ",fs.errorString());
              }else{
                  QString line = fs.readLine(); // in = your QTextStream
                      ui->lw_lot_nbr_fd1->addItem(line);
                  while (!line.isNull())
                  {
                      line = fs.readLine();
                      //tempstr =  QString::split('"');
                      if(line.startsWith("""))
                      {
                          line.remove(0,1);
                      }
                      if(line.endsWith("""))
                      {
                          line.remove(line.size()-1,1);
                      }
          
          

          The line.startsWith("""); does not evaluate to true even though there is a quote there.
          the line.endsWith("""); does not evaluate to true even though there is a quote there.
          If I change to line.endsWith("\r") or line.endsWith("\n") ; does not evaluate to true even though there is a return and new line there.

          The strings are read from a text file. Each line is a number as:
          00001
          00002
          00003
          00004 etc.

          When debugging and I hover over the variable "line" it says "00001\r\n

          When displayed in my QListWidget the leading quote is there as is a tiny little square

          A hexdump of the text file gives:
          0000000 3030 3030 0d31 300a 3030 3230 0a0d 3030
          0000010 3030 0d33 300a 3030 3430 0a0d 3030 3030
          0000020 0d35 300a 3030 3630 0a0d 3030 3030 0d37
          0000030 300a 3030 3830 0a0d 3030 3030 0d39 300a
          0000040 3030 3031 0a0d 3030 3130 0d31 300a 3030
          0000050 3231 0a0d 3030 3130 0d33 300a 3030 3431
          0000060 0a0d 3030 3130 0d35 300a 3030 3631 0a0d
          0000070 3030 3130 0d37 300a 3030 3831 0a0d 3030
          0000080 3130 0d39 300a 3030 3032 0a0d 3030 3230
          0000090 0d31 300a 3030 3232 0a0d 3030 3230 0d33
          00000a0 300a 3030 3432 0a0d 3030 3230 0d35 300a

          The text file originally came from a windows system, is this part of the problem.

          Thanks for any help

          emp1953

          E Offline
          E Offline
          emp1953
          wrote on 13 Jan 2020, 02:29 last edited by
          #3

          @emp1953 I'm assuming that the 0a0d in the hex dump is the carriage return and line feed?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 13 Jan 2020, 05:54 last edited by
            #4

            I would go with QString::trimmed()

            Your hexdump doesn't look like a windows text file and does not match your expected strings

            3030 3030 0d31 300a 3030 3230 0a0d 3030
            0 0 0 0 <CR> 1 0 <LF> 0 0 2 0 <CR><LF> 0 0

            This does not look correct...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            E 1 Reply Last reply 14 Jan 2020, 21:16
            4
            • E emp1953
              13 Jan 2020, 01:49

              I have the following code:

              QString pathname = QDir::currentPath();
                  QString fpath = pathname + fname;
                  QFile fs(fpath);
              
                  int counter;
                  if(!fs.open(QIODevice::ReadOnly))
                  {
                      QMessageBox::information(0,"file info ",fs.errorString());
                  }else{
                      QString line = fs.readLine(); // in = your QTextStream
                          ui->lw_lot_nbr_fd1->addItem(line);
                      while (!line.isNull())
                      {
                          line = fs.readLine();
                          //tempstr =  QString::split('"');
                          if(line.startsWith("""))
                          {
                              line.remove(0,1);
                          }
                          if(line.endsWith("""))
                          {
                              line.remove(line.size()-1,1);
                          }
              
              

              The line.startsWith("""); does not evaluate to true even though there is a quote there.
              the line.endsWith("""); does not evaluate to true even though there is a quote there.
              If I change to line.endsWith("\r") or line.endsWith("\n") ; does not evaluate to true even though there is a return and new line there.

              The strings are read from a text file. Each line is a number as:
              00001
              00002
              00003
              00004 etc.

              When debugging and I hover over the variable "line" it says "00001\r\n

              When displayed in my QListWidget the leading quote is there as is a tiny little square

              A hexdump of the text file gives:
              0000000 3030 3030 0d31 300a 3030 3230 0a0d 3030
              0000010 3030 0d33 300a 3030 3430 0a0d 3030 3030
              0000020 0d35 300a 3030 3630 0a0d 3030 3030 0d37
              0000030 300a 3030 3830 0a0d 3030 3030 0d39 300a
              0000040 3030 3031 0a0d 3030 3130 0d31 300a 3030
              0000050 3231 0a0d 3030 3130 0d33 300a 3030 3431
              0000060 0a0d 3030 3130 0d35 300a 3030 3631 0a0d
              0000070 3030 3130 0d37 300a 3030 3831 0a0d 3030
              0000080 3130 0d39 300a 3030 3032 0a0d 3030 3230
              0000090 0d31 300a 3030 3232 0a0d 3030 3230 0d33
              00000a0 300a 3030 3432 0a0d 3030 3230 0d35 300a

              The text file originally came from a windows system, is this part of the problem.

              Thanks for any help

              emp1953

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 13 Jan 2020, 07:04 last edited by
              #5

              @emp1953 said in How to remove quotes and eol characters from QString?:

              if(line.startsWith("""))

              That will not even compile. You probably wanted: if(line.startsWith("\""))...

              Regards

              Qt has to stay free or it will die.

              E 2 Replies Last reply 14 Jan 2020, 21:02
              5
              • A aha_1980
                13 Jan 2020, 07:04

                @emp1953 said in How to remove quotes and eol characters from QString?:

                if(line.startsWith("""))

                That will not even compile. You probably wanted: if(line.startsWith("\""))...

                Regards

                E Offline
                E Offline
                emp1953
                wrote on 14 Jan 2020, 21:02 last edited by
                #6

                @aha_1980 Interesting that you say this won't compile. From the command line it does not compile and throws an error on this line. From within the Qt Creator development environment it does in fact compile without warnings and outputs an executable. I have found that my make from the command line uses a different version of gcc than does the QtCreator development environment. An interesting turn of events.

                1 Reply Last reply
                0
                • A aha_1980
                  13 Jan 2020, 07:04

                  @emp1953 said in How to remove quotes and eol characters from QString?:

                  if(line.startsWith("""))

                  That will not even compile. You probably wanted: if(line.startsWith("\""))...

                  Regards

                  E Offline
                  E Offline
                  emp1953
                  wrote on 14 Jan 2020, 21:03 last edited by
                  #7

                  @aha_1980Even with this change to ---> if(line.startsWith(""")) it still does not evaluate to true.

                  1 Reply Last reply
                  0
                  • C Christian Ehrlicher
                    13 Jan 2020, 05:54

                    I would go with QString::trimmed()

                    Your hexdump doesn't look like a windows text file and does not match your expected strings

                    3030 3030 0d31 300a 3030 3230 0a0d 3030
                    0 0 0 0 <CR> 1 0 <LF> 0 0 2 0 <CR><LF> 0 0

                    This does not look correct...

                    E Offline
                    E Offline
                    emp1953
                    wrote on 14 Jan 2020, 21:16 last edited by
                    #8

                    @Christian-Ehrlicher The file is a column of lot numbers 00001 through 09999.
                    It was created in microsoft excel then highlighted and copied into notepad, saved as a .txt file.
                    Moved to the Linux environment. The hexdump was of this file. I implemented the trimmed() functionality which didn't change anything in my output. I'm reading each lot number into a QString line variable and putting it into a QListWidget using .AddItem.
                    There is still the annoying little "square" shape after every item in the QListWidget. Is that Qt trying to figure out what the \r\n is? and the square is its best guess?
                    How could I strip those out using a QRegExp???

                    Thanks for everyones help

                    A 1 Reply Last reply 15 Jan 2020, 07:30
                    0
                    • E emp1953
                      14 Jan 2020, 21:16

                      @Christian-Ehrlicher The file is a column of lot numbers 00001 through 09999.
                      It was created in microsoft excel then highlighted and copied into notepad, saved as a .txt file.
                      Moved to the Linux environment. The hexdump was of this file. I implemented the trimmed() functionality which didn't change anything in my output. I'm reading each lot number into a QString line variable and putting it into a QListWidget using .AddItem.
                      There is still the annoying little "square" shape after every item in the QListWidget. Is that Qt trying to figure out what the \r\n is? and the square is its best guess?
                      How could I strip those out using a QRegExp???

                      Thanks for everyones help

                      A Offline
                      A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on 15 Jan 2020, 07:30 last edited by aha_1980
                      #9

                      @emp1953 said in How to remove quotes and eol characters from QString?:

                      There is still the annoying little "square" shape after every item in the QListWidget.

                      QFile::readLine() behaves different on Windows and Linux, your can read that in the docu.

                      On Linux it assumes \n line endings, on Windows \r\n ones, which it replaces with \n.

                      Your file is none of these, it has mixed endings. If you want to use it in Linux, you have to normalize the line endings first. Otherwise readLine() will not do what you expect and every following procedure will fail.

                      The square bracket you see is probably the \r char, which cannot be displayed otherwise.

                      From within the Qt Creator development environment it does in fact compile without warnings and outputs an executable.

                      But not the code you posted above. This code is invalid and no compiler will accept it. Double check what you are doing.

                      Regards

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      2

                      3/9

                      13 Jan 2020, 02:29

                      topic:navigator.unread, 6
                      • Login

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