How replaceAll "/" in java String to format URL?

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( at java.util.regex.Matcher.appendReplacement( at java.util.regex.Matcher.replaceAll( at java.lang.String.replaceAll( ------------------------------------------ **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, '\\'));
What error? That seems to work for me

以上就是How replaceAll "/" in java String to format URL?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准