Problem with string.replaceAll

I'm just trying to replace the string \\ with \\\\

Below is the program, but it ends

 String path="\\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material"; long start = System.currentTimeMillis(); // replace this string \\ with \\\\ String formatedPath = path.replaceAll("\\\\", "\\\\\\\\"); System.out.println(" string after formatting using replaceAll = "+formatedPath); long end = System.currentTimeMillis(); System.out.println(" time take in milli seconds for String.replaceAll = "+Long.toString(end-start) ); 

Please let me know the mistake I made.

+4
source share
6 answers

To literally replace a string where you do not need the power of regular expressions, you should use replace , not replaceAll , because it is simpler and more efficient.

 // replace single backslash with double String formatedPath = path.replace("\\", "\\\\"); 
+3
source

Your real line contains only one \. Test

 System.out.println("\\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material"); 

So your replaceAll works fine if it returns double \\ when System.out.println ("your string" .replaceAll (...));

+1
source

Try it like

 String path = "\\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material"; long start = System.currentTimeMillis(); // replace this string \\ with \\\\ String formatedPath = path.replaceAll("\\\\", "\\\\\\\\\\\\\\\\"); System.out.println(" string after formatting using replaceAll = " + formatedPath); long end = System.currentTimeMillis(); System.out.println(" time take in milli seconds for String.replaceAll = " + Long.toString(end - start)); System.out.println(" path "+formatedPath); 
+1
source

on your line when you say \ it actually means \ shielded by another \, so it replaces it correctly.

 String path="\\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material"; System.out.println("String before: "+ path); long start = System.currentTimeMillis(); // replace this string \\ with \\\\ String formatedPath = path.replaceAll("\\\\", "\\\\\\\\"); System.out.println(" string after formatting using replaceAll = "+formatedPath); 

I got the conclusion

  String before: \dctmadmin\Human Resource\Training\Procedures\Formalities\Legalities\Material string after formatting using replaceAll = \\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material time take in milli seconds for String.replaceAll = 2 
0
source

You did the right thing. The Java \\ line displays one backslash; in a regular expression, this is a non-String escape.

  String formatedPath = path.replaceAll("\\\\", "\\\\\\\\"); System.out.println("path = " + path); System.out.println("formatedPath = " + formatedPath); 

gives

 path = \dctmadmin\Human Resource\Training\Pr...s\Form...s\L...s\Material formatedPath = \\dctmadmin\\Human Resource\\Training\\Pr..s\\Form...s\\Material 
0
source

Output

 string after formatting using replaceAll = \\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material 

is correct. It seems that it has not changed, because you refuse the fact that the "\" in your original is escaped :)

0
source

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


All Articles