Reputation: 529
Assume I have a string input as
String str = "xxx---xxx\\\'hi\'xx";
When I try to print the string, it becomes below because of the backslash:
xxx---xxx\'hi'xx
How can I make the output still as
xxx---xxx\\\'hi\'xx
Upvotes: 0
Views: 748
Reputation: 148890
When the java compiler reads a literal string in a source file, it knows that the backslash
is a special quoting character that is user to handle literaly the following character even if is was special, including the backslash itself.
So when you write String s = 'a\\b';
you have defined s
as the string containing 3 characters, a
, \
and b
.
So if you want a number or backslash characters in a string, just write twice this number in your source code.
Upvotes: 1
Reputation: 1
Backslash on its own is an escape character. Read more about it here.
In your case, you will have to print "xxx---xxx\\\\\\'hi\\'xx"
to get the output as "xxx---xxx\\\'hi\'xx"
. You may also find and replace all "\"
with "\\"
in your string for automation.
Upvotes: 0
Reputation: 421
String str = "xxx---xxx\\\\\\'hi\\'xx";
- will return your desired output
Upvotes: 0