JavaGeek
JavaGeek

Reputation: 1529

Issue with string.replaceAll

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

Below is the program but it is terminating

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 was doing.

Upvotes: 4

Views: 152

Answers (6)

robert
robert

Reputation: 4867

Output of

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

is correct. It looks like it hasn't changed because you're discounting the fact that the '\' in your original are escaped :)

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

You did right. In Java string \\ represents one backslash, in regex it is the 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

Upvotes: 0

Jacob
Jacob

Reputation: 14731

Try as

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);

Upvotes: 1

Ian Roberts
Ian Roberts

Reputation: 122364

For a literal string replacement where you don't need the power of regular expressions you should use replace rather than replaceAll as it's simpler and more efficient.

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

Upvotes: 3

Chuidiang
Chuidiang

Reputation: 1055

Your real String contains only one \. Test

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

So your replaceAll is running OK if it returns double \\ when System.out.println("your string".replaceAll(...));

Upvotes: 1

PermGenError
PermGenError

Reputation: 46408

in your string when you say \ it actually means \ escaped by another \, thus it is replacing 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);

output i got

   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

Upvotes: 0

Related Questions