How can I replace \ with / with Java?

I tried using the following regex but it didn't work.

myString.replaceAll ("\", "/");

An exception:

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \ ^ at java.util.regex.Pattern.error (Unknown Source) in java.util.regex.Pattern.compile (Unknown Source) in java.util. regex.Pattern (Unknown. Source) in java.util.regex.Pattern.compile (Unknown Source) in java.lang.String.replaceAll (Unknown Source)

+3
source share
2 answers

Your code does not even compile.

"\" ", . String h = "\"hello\""; h "hello".

, "\\" ( ), . . , "\\d+" ( ).


, . replace(char, char) , all.

myString.replace('\\', '/');

, , File.separator, .

+7

4 , escape .

myString.replaceAll("\\\\", "/");
+3

Source: https://habr.com/ru/post/1782405/


All Articles