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. Weird behavior using QString::replace and escape sequences

Weird behavior using QString::replace and escape sequences

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 13.8k 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.
  • B Offline
    B Offline
    bartikus_
    wrote on 25 Aug 2016, 14:05 last edited by
    #1

    Hello,

    I am experiencing some strange behavior when I am using QString::replace. Consider the following:

    QString str("some\\string");
    
    str.replace("\\","\");
    
    qDebug() << str;
    

    The code above obviously won't compile, as you can see.

    Basically I have a QString with double backslashes and I need to replace the double backslashes with single backslashes. The syntax is important in this case because my string is serial data which is being sent to a printer to print labels, so I have no flexibility in that regard.

    I actually assumed that I would need to do this:

    str.replace("\\\\","\\");
    

    but the string remains the same because there are no occurrences of "\\\\" in my string.

    If I try:

    str.replace("\\", "<placeholder>");
    

    it correctly does the replace. I then try:

    str.replace("\\","<placeholder>");
    str.replace("<placeholder>", "\");
    

    (but again this won't compile) and it does not work.

    Furthermore, every "\\" in my string is coincidentally followed by a '^'.

    If I do:

    str.replace("\\^","<placeholder>^");
    str.replace("<placeholder>^","\^");
    

    I get a compiler warning saying unknown escape sequence '\^', but it does compile and what I get is my original string with both "\\" removed. (So my example string goes from some\\^string to some^string, when my desired output is some\^string).

    Lastly, if I try:

    str.replace("\\^","<placeholder>^");
    str.replace("<placeholder>^","\\^");
    

    or more simply:

    str.replace("\\^","\\^");
    

    The string remains unchanged.

    I've seen posts trying to replace "\" characters with "/" for file paths, etc. but nothing has helped me get to the bottom of this. I feel like I'm taking crazy pills. Any help would be immensely appreciated.

    K 1 Reply Last reply 25 Aug 2016, 14:19
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 25 Aug 2016, 14:13 last edited by
      #2

      might be a bug... would need to check the source code.

      As a workaround you could use QRegularExpression and do the replacement in a loop by specifying the startIndex and matched length.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      B 1 Reply Last reply 25 Aug 2016, 14:18
      1
      • R raven-worx
        25 Aug 2016, 14:13

        might be a bug... would need to check the source code.

        As a workaround you could use QRegularExpression and do the replacement in a loop by specifying the startIndex and matched length.

        B Offline
        B Offline
        bartikus_
        wrote on 25 Aug 2016, 14:18 last edited by
        #3

        @raven-worx said in Weird behavior using QString::replace and escape sequences:

        might be a bug... would need to check the source code.

        As a workaround you could use QRegularExpression and do the replacement in a loop by specifying the startIndex and matched length.

        I'll try this. By the way, I'm using Qt 5.7 if you need to know to check the source code.

        1 Reply Last reply
        0
        • B bartikus_
          25 Aug 2016, 14:05

          Hello,

          I am experiencing some strange behavior when I am using QString::replace. Consider the following:

          QString str("some\\string");
          
          str.replace("\\","\");
          
          qDebug() << str;
          

          The code above obviously won't compile, as you can see.

          Basically I have a QString with double backslashes and I need to replace the double backslashes with single backslashes. The syntax is important in this case because my string is serial data which is being sent to a printer to print labels, so I have no flexibility in that regard.

          I actually assumed that I would need to do this:

          str.replace("\\\\","\\");
          

          but the string remains the same because there are no occurrences of "\\\\" in my string.

          If I try:

          str.replace("\\", "<placeholder>");
          

          it correctly does the replace. I then try:

          str.replace("\\","<placeholder>");
          str.replace("<placeholder>", "\");
          

          (but again this won't compile) and it does not work.

          Furthermore, every "\\" in my string is coincidentally followed by a '^'.

          If I do:

          str.replace("\\^","<placeholder>^");
          str.replace("<placeholder>^","\^");
          

          I get a compiler warning saying unknown escape sequence '\^', but it does compile and what I get is my original string with both "\\" removed. (So my example string goes from some\\^string to some^string, when my desired output is some\^string).

          Lastly, if I try:

          str.replace("\\^","<placeholder>^");
          str.replace("<placeholder>^","\\^");
          

          or more simply:

          str.replace("\\^","\\^");
          

          The string remains unchanged.

          I've seen posts trying to replace "\" characters with "/" for file paths, etc. but nothing has helped me get to the bottom of this. I feel like I'm taking crazy pills. Any help would be immensely appreciated.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 25 Aug 2016, 14:19 last edited by
          #4

          @bartikus_

          Hello,
          \ is an escape character inside string literals and it can't just be sitting on its own, that's why you get the compiler errors. If you have \ in your string, then you encode that as "\\" in the string literals, double backslashes go as "\\\\" and so on.
          Just escape all the characters properly and it should be working okay:

          str.replace("\\\\^","\\^"); // Replace \\^ with \^
          

          Kind regards.

          Read and abide by the Qt Code of Conduct

          B 1 Reply Last reply 25 Aug 2016, 14:32
          1
          • K kshegunov
            25 Aug 2016, 14:19

            @bartikus_

            Hello,
            \ is an escape character inside string literals and it can't just be sitting on its own, that's why you get the compiler errors. If you have \ in your string, then you encode that as "\\" in the string literals, double backslashes go as "\\\\" and so on.
            Just escape all the characters properly and it should be working okay:

            str.replace("\\\\^","\\^"); // Replace \\^ with \^
            

            Kind regards.

            B Offline
            B Offline
            bartikus_
            wrote on 25 Aug 2016, 14:32 last edited by
            #5

            @kshegunov said in Weird behavior using QString::replace and escape sequences:

            \ is an escape character inside string literals and it can't just be sitting on its own, that's why you get the compiler errors. If you have \ in your string, then you encode that as "\\" in the string literals, double backslashes go as "\\\\" and so on.
            Just escape all the characters properly and it should be working okay:

            str.replace("\\\\^","\\^"); // Replace \\^ with \^
            

            Thank you for your post. Yes, I'm well aware of escape characters and how they work. The point of my post is that it is not making sense to me what I am observing. What you are suggesting is exactly how I expected it to work--but it does not. This is why I am going crazy over here.

            K 1 Reply Last reply 25 Aug 2016, 14:39
            0
            • B bartikus_
              25 Aug 2016, 14:32

              @kshegunov said in Weird behavior using QString::replace and escape sequences:

              \ is an escape character inside string literals and it can't just be sitting on its own, that's why you get the compiler errors. If you have \ in your string, then you encode that as "\\" in the string literals, double backslashes go as "\\\\" and so on.
              Just escape all the characters properly and it should be working okay:

              str.replace("\\\\^","\\^"); // Replace \\^ with \^
              

              Thank you for your post. Yes, I'm well aware of escape characters and how they work. The point of my post is that it is not making sense to me what I am observing. What you are suggesting is exactly how I expected it to work--but it does not. This is why I am going crazy over here.

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 25 Aug 2016, 14:39 last edited by
              #6

              @bartikus_
              Then provide a sample string and what you expect to get in the end.

              Read and abide by the Qt Code of Conduct

              B 1 Reply Last reply 25 Aug 2016, 14:51
              1
              • K kshegunov
                25 Aug 2016, 14:39

                @bartikus_
                Then provide a sample string and what you expect to get in the end.

                B Offline
                B Offline
                bartikus_
                wrote on 25 Aug 2016, 14:51 last edited by
                #7

                @kshegunov said in Weird behavior using QString::replace and escape sequences:

                @bartikus_
                Then provide a sample string and what you expect to get in the end.

                I believe I have provided both of your requests in my original post.

                K 1 Reply Last reply 25 Aug 2016, 14:54
                0
                • B bartikus_
                  25 Aug 2016, 14:51

                  @kshegunov said in Weird behavior using QString::replace and escape sequences:

                  @bartikus_
                  Then provide a sample string and what you expect to get in the end.

                  I believe I have provided both of your requests in my original post.

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 25 Aug 2016, 14:54 last edited by
                  #8

                  @bartikus_

                  QString str("some\\string");
                  

                  This is equivalent to some\string, so what is supposed to be replaced with what?

                  Read and abide by the Qt Code of Conduct

                  B 1 Reply Last reply 25 Aug 2016, 15:09
                  1
                  • K kshegunov
                    25 Aug 2016, 14:54

                    @bartikus_

                    QString str("some\\string");
                    

                    This is equivalent to some\string, so what is supposed to be replaced with what?

                    B Offline
                    B Offline
                    bartikus_
                    wrote on 25 Aug 2016, 15:09 last edited by
                    #9

                    @kshegunov Ah, okay. In simplifying I think I made it more confusing. Ultimately what I have is data coming out of a raspberry pi serial port that I can monitor with a terminal application such as terra term on my windows PC. What I see in terra term being transmitted is double backslashes, and I need to reduce them to be a single backslash, as the ZPL printer syntax requires. The data I have is originally in a QString, then converted to a QByteArray using QString::toUtf8() and sent out the serial port. If I use qDebug to print my QString to the console, the text displayed has two backslashes, the same as what I see externally on terra term. I'm not sure if this information helps you, but worth a shot.

                    K 1 Reply Last reply 25 Aug 2016, 15:16
                    1
                    • B bartikus_
                      25 Aug 2016, 15:09

                      @kshegunov Ah, okay. In simplifying I think I made it more confusing. Ultimately what I have is data coming out of a raspberry pi serial port that I can monitor with a terminal application such as terra term on my windows PC. What I see in terra term being transmitted is double backslashes, and I need to reduce them to be a single backslash, as the ZPL printer syntax requires. The data I have is originally in a QString, then converted to a QByteArray using QString::toUtf8() and sent out the serial port. If I use qDebug to print my QString to the console, the text displayed has two backslashes, the same as what I see externally on terra term. I'm not sure if this information helps you, but worth a shot.

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 25 Aug 2016, 15:16 last edited by kshegunov
                      #10

                      @bartikus_

                      It actually does. qDebug will escape the string before printing on the standard output, so you'll see a double backslash for each single backslash. For example:

                      qDebug() << QStringLiteral("String with\\a backslash");
                      

                      will show: "String with\\a backslash", even though the string contains a single backslash. The following:

                      QTextStream out(stdout);
                      out << QStringLiteral("String with\\a backslash");
                      

                      on the other hand, prints String with\a backslash as expected.

                      Read and abide by the Qt Code of Conduct

                      B 1 Reply Last reply 25 Aug 2016, 15:22
                      2
                      • K kshegunov
                        25 Aug 2016, 15:16

                        @bartikus_

                        It actually does. qDebug will escape the string before printing on the standard output, so you'll see a double backslash for each single backslash. For example:

                        qDebug() << QStringLiteral("String with\\a backslash");
                        

                        will show: "String with\\a backslash", even though the string contains a single backslash. The following:

                        QTextStream out(stdout);
                        out << QStringLiteral("String with\\a backslash");
                        

                        on the other hand, prints String with\a backslash as expected.

                        B Offline
                        B Offline
                        bartikus_
                        wrote on 25 Aug 2016, 15:22 last edited by
                        #11

                        @kshegunov Actually, I stand corrected. Thanks to your clarification on qDebug, I scratched my head for a bit then re-ran my test with terra term. The output to terra term does NOT have double backslashes. They are single backslashes, as required. It appears that the backslashes are not the root of my problem. Thank you for your help.

                        K 1 Reply Last reply 25 Aug 2016, 15:32
                        2
                        • B Offline
                          B Offline
                          bartikus_
                          wrote on 25 Aug 2016, 15:27 last edited by
                          #12

                          My problem (can't print) is not solved, but I am marking this topic as solved as it is likely not a Qt issue.

                          1 Reply Last reply
                          0
                          • B bartikus_
                            25 Aug 2016, 15:22

                            @kshegunov Actually, I stand corrected. Thanks to your clarification on qDebug, I scratched my head for a bit then re-ran my test with terra term. The output to terra term does NOT have double backslashes. They are single backslashes, as required. It appears that the backslashes are not the root of my problem. Thank you for your help.

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 25 Aug 2016, 15:32 last edited by
                            #13

                            @bartikus_

                            Thank you for your help.

                            You're welcome

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            1
                            • VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on 25 Aug 2016, 17:07 last edited by
                              #14

                              Just to add to the discussion, these cases are exactly where c++11 raw strings help a lot https://en.wikipedia.org/wiki/C%2B%2B11#New_string_literals

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              1 Reply Last reply
                              1

                              2/14

                              25 Aug 2016, 14:13

                              topic:navigator.unread, 12
                              • Login

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