troubleshooting Question

Problem with ReplaceAll in String

Avatar of sangameshrh
sangameshrh asked on
Java
16 Comments1 Solution1326 ViewsLast Modified:
I want to replace all back slashes(\) in a string with %5C.
my code looks like
String str      =      "sandeep + kumar";
str                  =      str.replaceAll("\\","%5C");
System.out.println(str);

I am getting this error
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
    at java.util.regex.Pattern.error(Pattern.java:1700)
    at java.util.regex.Pattern.compile(Pattern.java:1453)
    at java.util.regex.Pattern.<init>(Pattern.java:1130)
    at java.util.regex.Pattern.compile(Pattern.java:822)
    at java.lang.String.replaceAll(String.java:2190)
    at Replace.<init>(Replace.java:31)
    at Replace.main(Replace.java:42)

Process completed.


And If I try with
str                  =      str.replaceAll("\\\\","%5C");

Output comes as
%7Cs%7Ca%7Cn%7Cd%7Ce%7Ce%7Cp%7C %7C%2B%7C %7Ck%7Cu%7Cm%7Ca%7Cr%7C%%7C2%7C4%7C

Thanks.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros