troubleshooting Question

Problem with ReplaceAll in String

Avatar of sangameshrh
sangameshrh asked on
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");

I am getting this error
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
    at java.util.regex.Pattern.error(
    at java.util.regex.Pattern.compile(
    at java.util.regex.Pattern.<init>(
    at java.util.regex.Pattern.compile(
    at java.lang.String.replaceAll(
    at Replace.<init>(
    at Replace.main(

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

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