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.0k 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.
  • kshegunovK kshegunov

    @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 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.

    kshegunovK 1 Reply Last reply
    0
    • B bartikus_

      @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.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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
      1
      • kshegunovK kshegunov

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

        B Offline
        B Offline
        bartikus_
        wrote on 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.

        kshegunovK 1 Reply Last reply
        0
        • B bartikus_

          @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.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on 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
          1
          • kshegunovK kshegunov

            @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 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.

            kshegunovK 1 Reply Last reply
            1
            • B bartikus_

              @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.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on 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
              2
              • kshegunovK kshegunov

                @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 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.

                kshegunovK 1 Reply Last reply
                2
                • B Offline
                  B Offline
                  bartikus_
                  wrote on 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_

                    @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.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on 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 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

                      • Login

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