Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:18:07+00:00 2026-06-15T10:18:07+00:00

In Java, when I do: a/b/c/d.replaceAll(/, @); I get back a@b@c@d But when I

  • 0

In Java, when I do:

    "a/b/c/d".replaceAll("/", "@");

I get back

    a@b@c@d

But when I do:

    "a/b/c/d".replaceAll("/", File.separator);

It throws a StringIndexOutOfBoundsException, and I don’t know why. I tried looking this up, but it wasn’t very helpful. Can anyone help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:18:09+00:00Added an answer on June 15, 2026 at 10:18 am

    It says it right there in the documentation:

    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.

    And, in Matcher.replaceAll:

    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. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

    What you need to do is to escape any escape characters you have in the replacement string, such as with Matcher.quoteReplacement():

    import java.io.File;
    import java.util.regex.Matcher;
    
    class Test {
        public static void main(String[] args) {
            String s = "a/b/c/d";
            String sep = "\\"; // File.separator;
            s = s.replaceAll("/", Matcher.quoteReplacement(sep));
            System.out.println(s);
        }
    }
    

    Note, I’m using the literal \\ in sep rather than using File.separator directly since my separator is the UNIX one – you should be able to just use:

    s = s.replaceAll("/", Matcher.quoteReplacement(File.separator));
    

    This outputs:

    a\b\c\d
    

    as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried with following regex, but it didn't work. myString.replaceAll(\, /); Exception: java.util.regex.PatternSyntaxException: Unexpected
I am trying to open this file in java and i want to know
I get java.lang.IndexOutOfBoundsException , when I call replaceAll() with replacement text containing $1 :
I'm working on code for a Java project, I currently have replaceAll(String, int) in
I'm having problems with a Java Regex Pattern Syntax expression. toCensor = toCensor.replaceAll((new StringBuilder(((?i)))).append(word).toString(),
Java doesn't support multiple inheritance for abstract classes, but I'm trying to do the
Java, how do I create a hash map outside of main but refer to
I need to use Java to replace some JavaScript in a file. The steps
I have a java string of form a_x,b_y,c_z. I would like to get rid
In Java, when doing a replaceAll to look for a regex pattern like: replaceAll(\\?i\\b(\\w+)\\b(\\s+\\1)+\\b,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.