Java, how to replace backslash?

In java, I have a file path, for example 'C: \ A \ B \ C', I want it to be changed to '' C: / A / B / C '. how to replace backslash?

+3
source share
5 answers
String text = "C:\\A\\B\\C"; String newString = text.replace("\\", "/"); System.out.println(newString); 
+13
source

Since you requested a regular expression, you will have to escape the \ character several times:

 String path = "c:\\A\\B\\C"; System.out.println(path.replaceAll("\\\\", "/")); 
+8
source

You can do this using the String.replace method:

 public static void main(String[] args) { String foo = "C:\\foo\\bar"; String newfoo = foo.replace("\\", "/"); System.out.println(newfoo); } 
+1
source

To replace all occurrences of a given character:

 String result = candidate.replace( '\\', '/' ); 

Regards, Cyril

0
source
 String oldPath = "C:\\A\\B\\C"; String newPath = oldPath.replace('\\', '/'); 
0
source

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


All Articles