Weird behavior using QString::replace and escape sequences
-
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