4

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.

13
  • 2
    Terminating? Do you get any errors? Commented Nov 2, 2012 at 8:45
  • Long.toString(end-start) is not neccessary. (end-start) will suffice. Commented Nov 2, 2012 at 8:48
  • Its giving Thread[main] (suspended Exception PatternSyntaxException) Commented Nov 2, 2012 at 8:50
  • Since you don't require any regex functionality, think about using String.replace(String, String) instead of String.replaceAll(String, String) Commented Nov 2, 2012 at 8:50
  • In your String, all \\ are really one \ inside the String. Your replaceAll gives a String with two real \\, so it´s OK. Test doing System.out.println("\\dctmadmin\\Human Resource\\Training\\Procedures\\Formalities\\Legalities\\Material") and System.out.println("\\drt...\\Material".replaceAll(...)) Commented Nov 2, 2012 at 8:53

6 Answers 6

3

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("\\", "\\\\");
Sign up to request clarification or add additional context in comments.

2 Comments

Yes. It works but is it possible to display output as \\\\dctmadmin\\\\Human Resource\\\\Training\\\\Procedures\\\\Formalities\\\\Legalities\\\\Material
as per Polppan suggestion i changed it to String formatedPath = path.replaceAll("\\\\", "\\\\\\\\\\\\\\\\"); and then its displaying \\\\dctmadmin\\\\Human Resource\\\\Training\\\\Procedures\\\\Formalities\\\\Legalities\\\\Material
1

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

Comments

1

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

1 Comment

Your cde works and displays output as \\\\dctmadmin\\\\Human Resource\\\\Training\\\\Procedures\\\\Formalities\\\\Legalities\\\\Material
0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.