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

Weird behavior using QString::replace and escape sequences

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 Posters 14.1k Views 2 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.
  • 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
                    • V Offline
                      V 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

                      14/14

                      25 Aug 2016, 17:07

                      • Login

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