Reputation: 1463
I have to make below statement as string.i am trying,but it's giving invalid character sequence.I know it is basic,But not able to do this.any help on this appreciated.
String str="_1";
'\str%' ESCAPE '\'
Output should be: '\_1%' ESCAPE '\'.
Thanks,
Chaitu
Upvotes: 0
Views: 3532
Reputation: 827
String str="_1";
String source = "'\\str%' ESCAPE '\\'";
String result = source.replaceAll("str", str);
Another way to implement string interpolation. The replaceAll
function finds all occurrences of str
in the source string and replaces them by the passed argument.
To encode the backslash \
in a Java string, you have to duplicate it, because a single backslash works as an escape character.
Beware that the first argument if replaceAll
is actually a regular expression, so some characters have a special meaning, but for simple words it will work as expected.
Upvotes: 1
Reputation: 1345
String str="_1";
String output = String.format("'\\%s%%' ESCAPE '\\'",str);
System.out.println(output);//prints '\_1%' ESCAPE '\'
Upvotes: 0
Reputation: 16703
Inside a string, a backslash character will "escape" the character after it - which causes that character to be treated differently.
Since \
has this special meaning, if you actually want the \
character itself in the string, you need to put \\
. The first backslash escapes the second, causing it to be treated as a literal \
inside the string.
Knowing this, you should be able to construct the resulting string you need. Hope this helps.
Upvotes: 1