1
String url = "d://test////hello\\\hello";
String separator = File.separator;
url = url.replaceAll("\\*", separator);
url = url.replaceAll("/+", separator);

I want to format those url, but error occurs when i attempt to use replaceAll("/+", separator). and i attempt to escaped "/" as "\\/", it still doesn't work..

This is the Exception from console:

Exception in thread "main" **java.lang.StringIndexOutOfBoundsException**: String index out of range: 1
    at java.lang.String.charAt(String.java:686)
    at java.util.regex.Matcher.appendReplacement(Matcher.java:703)
    at java.util.regex.Matcher.replaceAll(Matcher.java:813)
    at java.lang.String.replaceAll(String.java:2189)

Now it works

 String separator = null;
 if(File.separator.equals("/")) {
    separator = "/";
    url = url.replaceAll("/+", separator);
    url = url.replaceAll("\\\\+", separator);
 } else {
    separator = Matcher.quoteReplacement(File.separator);
    url = url.replaceAll("/+", separator);
    url = url.replaceAll("\\+", separator);
 }

:) it works in javascript

var i = "d:\\ad////df";
alert(i.replace(/\/+/g, '\\'));
2
  • 1
    What error? That seems to work for me Commented Mar 11, 2013 at 3:00
  • it is out of bound exception, try to check the java.lang.StringIndexOutOfBoundsException: String index out of range: 1 at java.lang.String.charAt(String.java:686) at Commented Mar 11, 2013 at 3:04

2 Answers 2

3

Your platform is Windows right? So File.separator will be a backslash right?

The explanation is that the 2nd argument of String.replaceAll is not a simple String. Rather it is a replacement pattern ...

The javadoc says:

"Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired. "

So your replacement String that consists of a single backslash is an invalid literal replacement string. You need to quote the separator String ... like the javadoc says.

(It is a little surprising that you get that particular exception. I can imagine how it could happen, but I'd have thought that they'd deal with bad escapes more elegantly. Mind you, if this was reported as a "bug", Oracle would probably not fix it. A fix would break backwards compatibility.)

Sign up to request clarification or add additional context in comments.

1 Comment

Follow your tips, now it works properly, Thank you very much.
3

Try:

url = url.replaceAll("\\\\+", separator);

You need 4 backward slashes. Escape once for Java string and once for regex meta-character. That is for regex you need two backward slashes \\, and in string you need to escape both of them with another two.

Also, the quantifier * means zero or more, you need to use +.

2 Comments

Thanks you, - - but it throws the same exception.
It works for me, but I am using Ubuntu. Check out @Stephen's answer also. The end result in my case is d:/test/hello/hello.

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.