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. replace the character ' with \' in the QString.
Forum Updated to NodeBB v4.3 + New Features

replace the character ' with \' in the QString.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 15.4k 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.
  • S Offline
    S Offline
    Sebastian
    wrote on last edited by Sebastian
    #1

    Dear Qt guys,
    May be this is really a silly question but i am still could not find the solutions.
    I have string there i have to replace some charaters.

    QString = Book + ' '+ Pen;
    

    I have to replace the " ' "with the characters " \ ' "

    I tried with lots of code

    formulaString.replace("'", "\'");
    formulaString.replace("\'", "\\'");
    formulaString.replace("\'", "/\'");
    

    but nothing worked.Please suggest me any solution if you have
    TIA

    K 1 Reply Last reply
    0
    • S Sebastian

      Dear Qt guys,
      May be this is really a silly question but i am still could not find the solutions.
      I have string there i have to replace some charaters.

      QString = Book + ' '+ Pen;
      

      I have to replace the " ' "with the characters " \ ' "

      I tried with lots of code

      formulaString.replace("'", "\'");
      formulaString.replace("\'", "\\'");
      formulaString.replace("\'", "/\'");
      

      but nothing worked.Please suggest me any solution if you have
      TIA

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Sebastian

      When you need to have a special character in string you have to prepend with a backslash as you know obviously already. The confusion comes typically because backslash is also such a character.

      formulaString.replace("\'", "\\\'");
      

      The examples of [QRegularExpressions](formulaString.replace("'", "\'") and playing a bit may help.

      Vote the answer(s) that helped you to solve your issue(s)

      S 1 Reply Last reply
      1
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        Was just wondering if there is not some help around.

        Check out QRegularExpression::escape

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        1
        • K koahnig

          @Sebastian

          When you need to have a special character in string you have to prepend with a backslash as you know obviously already. The confusion comes typically because backslash is also such a character.

          formulaString.replace("\'", "\\\'");
          

          The examples of [QRegularExpressions](formulaString.replace("'", "\'") and playing a bit may help.

          S Offline
          S Offline
          Sebastian
          wrote on last edited by
          #4

          @koahnig :Thanks for your reply.This i alreday tried.

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Since we all agreed that escape sequences are a pain in the 🍑 C++11 introduced raw string literals:

            formulaString.replace(R"(')", R"(\')");

            "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
            6
            • S Offline
              S Offline
              Sebastian
              wrote on last edited by
              #6

              Thanks guys i got the the solution.Thanks for your help.Qt provide the functionality what i was looking for.

              A 1 Reply Last reply
              0
              • S Sebastian

                Thanks guys i got the the solution.Thanks for your help.Qt provide the functionality what i was looking for.

                A Offline
                A Offline
                Akash1
                wrote on last edited by
                #7

                @Sebastian , could you please post your resolution here? I need this. Thanks in advanced,

                mrjjM 1 Reply Last reply
                0
                • A Akash1

                  @Sebastian , could you please post your resolution here? I need this. Thanks in advanced,

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Akash1
                  Its

                  formulaString.replace("\'", "\\\'");
                  

                  The odd part was that since \ is special too, it must also be escaped.

                  or

                  formulaString.replace(R"(')", R"(\')");
                  

                  which use the new raw strings and is good if many chars that need escaping.

                  1 Reply Last reply
                  4

                  • Login

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