Weird behavior using QString::replace and escape sequences
-
Hello,
I am experiencing some strange behavior when I am using QString::replace. Consider the following:
QString str("some\\string"); str.replace("\\","\"); qDebug() << str;
The code above obviously won't compile, as you can see.
Basically I have a QString with double backslashes and I need to replace the double backslashes with single backslashes. The syntax is important in this case because my string is serial data which is being sent to a printer to print labels, so I have no flexibility in that regard.
I actually assumed that I would need to do this:
str.replace("\\\\","\\");
but the string remains the same because there are no occurrences of "\\\\" in my string.
If I try:
str.replace("\\", "<placeholder>");
it correctly does the replace. I then try:
str.replace("\\","<placeholder>"); str.replace("<placeholder>", "\");
(but again this won't compile) and it does not work.
Furthermore, every "\\" in my string is coincidentally followed by a '^'.
If I do:
str.replace("\\^","<placeholder>^"); str.replace("<placeholder>^","\^");
I get a compiler warning saying unknown escape sequence '\^', but it does compile and what I get is my original string with both "\\" removed. (So my example string goes from some\\^string to some^string, when my desired output is some\^string).
Lastly, if I try:
str.replace("\\^","<placeholder>^"); str.replace("<placeholder>^","\\^");
or more simply:
str.replace("\\^","\\^");
The string remains unchanged.
I've seen posts trying to replace "\" characters with "/" for file paths, etc. but nothing has helped me get to the bottom of this. I feel like I'm taking crazy pills. Any help would be immensely appreciated.
-
might be a bug... would need to check the source code.
As a workaround you could use
QRegularExpression
and do the replacement in a loop by specifying the startIndex and matched length. -
might be a bug... would need to check the source code.
As a workaround you could use
QRegularExpression
and do the replacement in a loop by specifying the startIndex and matched length.@raven-worx said in Weird behavior using QString::replace and escape sequences:
might be a bug... would need to check the source code.
As a workaround you could use
QRegularExpression
and do the replacement in a loop by specifying the startIndex and matched length.I'll try this. By the way, I'm using Qt 5.7 if you need to know to check the source code.
-
Hello,
I am experiencing some strange behavior when I am using QString::replace. Consider the following:
QString str("some\\string"); str.replace("\\","\"); qDebug() << str;
The code above obviously won't compile, as you can see.
Basically I have a QString with double backslashes and I need to replace the double backslashes with single backslashes. The syntax is important in this case because my string is serial data which is being sent to a printer to print labels, so I have no flexibility in that regard.
I actually assumed that I would need to do this:
str.replace("\\\\","\\");
but the string remains the same because there are no occurrences of "\\\\" in my string.
If I try:
str.replace("\\", "<placeholder>");
it correctly does the replace. I then try:
str.replace("\\","<placeholder>"); str.replace("<placeholder>", "\");
(but again this won't compile) and it does not work.
Furthermore, every "\\" in my string is coincidentally followed by a '^'.
If I do:
str.replace("\\^","<placeholder>^"); str.replace("<placeholder>^","\^");
I get a compiler warning saying unknown escape sequence '\^', but it does compile and what I get is my original string with both "\\" removed. (So my example string goes from some\\^string to some^string, when my desired output is some\^string).
Lastly, if I try:
str.replace("\\^","<placeholder>^"); str.replace("<placeholder>^","\\^");
or more simply:
str.replace("\\^","\\^");
The string remains unchanged.
I've seen posts trying to replace "\" characters with "/" for file paths, etc. but nothing has helped me get to the bottom of this. I feel like I'm taking crazy pills. Any help would be immensely appreciated.
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.
-
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.
@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.
-
@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.
@bartikus_
Then provide a sample string and what you expect to get in the end. -
@bartikus_
Then provide a sample string and what you expect to get in the end.@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.
-
@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.
QString str("some\\string");
This is equivalent to
some\string
, so what is supposed to be replaced with what? -
QString str("some\\string");
This is equivalent to
some\string
, so what is supposed to be replaced with what?@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.
-
@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.
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. -
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.@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.
-
@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.
-
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