0

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

1
  • You've only shown one actual line of code. Please post a short but complete example instead. My guess is that you're looking for string interpolation in a way which really isn't supported in Java, but it's hard to tell for sure... Commented Apr 23, 2011 at 11:00

4 Answers 4

1
String result = "'\\" + str + "%' ESCAPE '\\'";
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1
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.

Comments

0
String str="_1";
String output = String.format("'\\%s%%' ESCAPE '\\'",str);
System.out.println(output);//prints '\_1%' ESCAPE '\'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.