Forum Stats

  • 3,734,860 Users
  • 2,247,061 Discussions
  • 7,857,529 Comments

Discussions

Escaping the backslash character in JSP

843840
843840 Member Posts: 49,995
The input String to my JSP can contain \ character, I need to normalize this \ character to words "back slash". I tried the following and it doesn't work:

normVal = normVal.replaceAll("\\\"," back slash ");

Can someone please tell me how i can get this to work?

Comments

  • 752426
    752426 Member Posts: 526
    Try this:
      normVal = normVal.replaceAll("\\\\"," back slash "); // Four back slashes
  • 843840
    843840 Member Posts: 49,995
    edited Jul 15, 2008 10:34AM
    Indeed.

    To start off, the backslash is an escape character in String, so you need to define it twice to represent an actual backslash: "\\". But since you're using [String#replaceAll()|http://java.sun.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)], which expects a regex pattern in the 1st parameter, you should re-escape the both backslashes once again, because the backslash itself is also an escape character in regex.

    If you don't want to use regex at all, then just use [String#replace()|http://java.sun.com/javase/6/docs/api/java/lang/String.html#replace(java.lang.CharSequence,%20java.lang.CharSequence)] instead.
    normVal = normVal.replace("\\"," back slash ");
  • 843840
    843840 Member Posts: 49,995
    Thanks Karanjit - that did work. However, it works only when the String i pass to my jsp (via a URL) has other special characters i.e. http://....mynormalization.jsp?value=$%5C! works and my jsp returns "dollar sign back slash exclamation mark".
    But if the \ is between alpha characters it doesn't work, i.e. if the String passed to my jsp is http://....mynormalization.jsp?value=check \ works
    where after the ASCII substitutions it is check%20%5C%20works, my jsp returns "check works" and not "check back slash works". Why does that happen?

    Incidentally "check%20%26%20works" (and all other spl. characters) works fine and my jsp returns "check and works".
  • 752426
    752426 Member Posts: 526
    SJQ wrote:
    it works only when the String i pass to my jsp (via a URL) has other special characters i.e. http://....mynormalization.jsp?value=$%5C! works and my jsp returns "dollar sign back slash exclamation mark".
    But if the \ is between alpha characters it doesn't work, i.e. if the String passed to my jsp is http://....mynormalization.jsp?value=check \ works
    where after the ASCII substitutions it is check%20%5C%20works, my jsp returns "check works" and not "check back slash works". Why does that happen?

    Incidentally "check%20%26%20works" (and all other spl. characters) works fine and my jsp returns "check and works".
    Following code works well.
     System.out.println("Check \\ Works".replaceAll("\\\\", "backSlash")); // prints Check backSlash Works
    Or,
     System.out.println("Check \\ Works".replace("\\", "backSlash")); // prints Check backSlash Works
    Please recheck your code.
  • 843840
    843840 Member Posts: 49,995
    Yes Karanjit there was indeed a bug in my code that sent String with alpha and \ to another loop. I fixed that now it works. Thanks for you help
This discussion has been closed.