How to detect escape sequence in a user-typed string [solved]
-
wrote on 20 Feb 2015, 17:33 last edited by
I'm just curious if there is a trick for interpreting escape sequences in a TextField.
In Qt's IDE, I can do something like this:
@QString x = "hello "bob"";@
The compile will preprocess the escape sequences so the output is:
@hello "bob"@
Which is great. But if I have a situation where the user types a string into a Qt textfield is there a simple solution for converting the string's escape sequences. So if I wanted to type this into the field:
@line 1\nline 2\nline 3@
Is there a easy way for QString to detect the escape sequence? Or do I have to parse the string myself in code and reconstruct it with the escape sequences?
-
Hi,
If you use QTextEdit then "toPlainText()":http://doc.qt.io/qt-5/qtextedit.html#plainText-prop gives the text as it is.
-
wrote on 21 Feb 2015, 16:14 last edited by
You could use QString.replace() and go through a list of common escape sequences. You need to make sure your search string isn't an escape sequence itself.
Example:
@
line 1\nline 2\nline 3line_1.replace("\n","\n");
line_1.replace("\t","\t");
etc.
@ -
wrote on 21 Feb 2015, 18:04 last edited by
Rondog, thats the answer I was looking for.
I had done this exact thing by splitting the string up and rebuiding the string. Your solution is much better!
I am learning that QString has all kinds of convenient ways of manipulating strings. You just brought a new one to my attention. Thanks!
-
Hi,
A regular expression might help you write less code for the replacements
1/5