I'm trying to replace the beginning of a string with backslashes to something else. For some weird reason the replaceAll function doesn't like backslashes.
String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replaceAll("\\\\xyz\\abc", "z:");
What should I do to solve this issue.
Thank you.
You need to double each backslash (again) as the Pattern class that is used by replaceAll() treats it as a special character:
String jarPath = "\\\\xyz\\abc\\wtf\\lame\\";
jarPath = jarPath.replaceAll("\\\\\\\\xyz\\\\abc", "z:");
A Java string treats backslash as an escape character so what replaceAll sees is: \\\\xyz\\abc
. But replaceAll also treats backslash as an escape character so the regular expression becomes the characters: \
\
x
y
z
\
a
b
c