I have a java string with "\\" character (extra '\' for escaping). I want to replace all the occurence of "\\" to "\". Any idea how it can be done? str.replaceAll("\\", "\") does not work. The problem is in replacing the \ character.
-
Is your string 'my\string' or 'my\\string'. Do you want to replace one or two slashes?Luchian Grigore– Luchian Grigore2011-12-02 10:45:20 +00:00Commented Dec 2, 2011 at 10:45
-
"my\string" is invalid in java. its definitely "my\\string".Anand– Anand2011-12-02 10:46:42 +00:00Commented Dec 2, 2011 at 10:46
-
I'm not talking about the java string. If in java you have String x = "my\\string", the actual logical string is "my\string", right?Luchian Grigore– Luchian Grigore2011-12-02 10:47:47 +00:00Commented Dec 2, 2011 at 10:47
-
@LuchianGrigore thats correct!Anand– Anand2011-12-02 10:48:59 +00:00Commented Dec 2, 2011 at 10:48
Add a comment
|
2 Answers
From the Java documentation:
Note that backslashes (
\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; seeMatcher.replaceAll. UseMatcher.quoteReplacement(java.lang.String)to suppress the special meaning of these characters, if desired.
1 Comment
sehe
Oh irony... fixed the escaping issues in your answer!