replace the character ' with \' in the QString.
-
wrote on 30 Aug 2018, 10:26 last edited by 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 -
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
TIAwrote on 30 Aug 2018, 10:39 last edited byWhen 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.
-
wrote on 30 Aug 2018, 10:43 last edited by
Was just wondering if there is not some help around.
Check out QRegularExpression::escape
-
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.
-
wrote on 30 Aug 2018, 10:55 last edited by
Since we all agreed that escape sequences are a pain in the 🍑 C++11 introduced raw string literals:
formulaString.replace(R"(')", R"(\')");
-
wrote on 3 Sept 2018, 11:58 last edited by
Thanks guys i got the the solution.Thanks for your help.Qt provide the functionality what i was looking for.
-
Thanks guys i got the the solution.Thanks for your help.Qt provide the functionality what i was looking for.
wrote on 29 Mar 2020, 04:11 last edited by@Sebastian , could you please post your resolution here? I need this. Thanks in advanced,
-
@Sebastian , could you please post your resolution here? I need this. Thanks in advanced,
@Akash1
ItsformulaString.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.