Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a StringIndexOutOfBoundsException when I try to replace `\\` with `\`?

Tags:

java

string

I have to replace \\ with \ in Java. The code I am using is

System.out.println( (MyConstants.LOCATION_PATH + File.separator + myObject.getStLocation() ).replaceAll("\\\\", "\\") );

But I don't know why it is throwing StringIndexOutOfBoundsException.

It says String index out of range: 1

What could be the reason? I guess it is because the first argument replaceAll accepts a pattern. What could be the possible solution?


Stacktrace

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(String.java:558)
    at java.util.regex.Matcher.appendReplacement(Matcher.java:696)
    at java.util.regex.Matcher.replaceAll(Matcher.java:806)
    at java.lang.String.replaceAll(String.java:2000)

Answer Found

asalamon74 posted the code I required, but I don't know why he deleted it. In any case here it is.

There is a bug already filed in Java's bug database. (Thanks for this reference, asalamon.)

yourString.replaceAll("\\\\", "\\\\");

Amazingly, both search and replace string are the same :) but still it does what I require.

like image 596
Rakesh Juyal Avatar asked Jun 26 '09 13:06

Rakesh Juyal


People also ask

How do you handle StringIndexOutOfBoundsException?

The StringIndexOutOfBoundsException is an exception in Java, and therefore can be handled using try-catch blocks using the following steps: Surround the statements that can throw an StringIndexOutOfBoundsException in try-catch blocks. Catch the StringIndexOutOfBoundsException.

How do I fix Java Lang StringIndexOutOfBoundsException string index out of range?

lang. StringIndexOutOfBoundsException: String index out of range: 0”, the java string may be an empty string. Check the string or character sequence value. Make sure the string contains value or add the empty string validation in the code.

What causes Java Lang IndexOutOfBoundsException?

The IndexOutOfBoundsException is thrown when attempting to access an invalid index within a collection, such as an array , vector , string , and so forth. It can also be implemented within custom classes to indicate invalid access was attempted for a collection.

How do you fix a string index out of the range?

The “TypeError: string index out of range” error is raised when you try to access an item at an index position that does not exist. You solve this error by making sure that your code treats strings as if they are indexed from the position 0.


6 Answers

Use String.replace instead of replaceAll to avoid it using a regex:

String original = MyConstants.LOCATION_PATH + File.seperator 
    + myObject.getStLocation();
System.out.println(original.replace("\\\\", "\\"));

Personally I wouldn't do it this way though - I'd create MyConstants.LOCATION_PATH_FILE as a File and then you could write:

File location = new File(MyConstants.LOCATION_PATH_FILE,
                         myObject.getStLocation());

which will do the right thing automatically.

like image 112
Jon Skeet Avatar answered Oct 05 '22 13:10

Jon Skeet


Well, i tried

    String test = "just a \\ test with some \\\\ and others \\\\ or \\ so";
    String result = test.replaceAll("\\\\", "\\\\");
    System.out.println(test);
    System.out.println(result);
    System.out.println(test.equals(result));

and got, as expected

just a \ test with some \\ and others \\ or \ so
just a \ test with some \\ and others \\ or \ so
true

What you really need is

string.replaceAll("\\\\\\\\", "\\\\");

to get

just a \ test with some \\ and others \\ or \ so
just a \ test with some \ and others \ or \ so
false

You want to find: \\  (2 slashes)
which needs to be escaped in the regex: \\\\ (4 slashes)
and escaped in Java: "\\\\\\\\" (8 slashes)
same for the replacement...

like image 21
user85421 Avatar answered Oct 05 '22 11:10

user85421


For the regex, if you want to change \ to \\, you should do this:

if (str.indexOf('\\') > -1)
    str = str.replaceAll("\\\\", "\\\\\\\\");
str = "\"" + str + "\"";

Where \\\\ means \ and \\\\\\\\ means \\.

like image 34
Shree ram Avatar answered Oct 05 '22 11:10

Shree ram


File.seperator is already escaped as is any string object so you are escaping them twice.

You only need to escape values that you are entering as a string literal.

like image 35
stevehipwell Avatar answered Oct 05 '22 11:10

stevehipwell


The best way is :

str.replace(**'**\\**'**, **'**/**'**); //with char method not String
like image 27
Thomas Neveu Avatar answered Oct 05 '22 13:10

Thomas Neveu


Try this

cadena.replaceAll("\\\\","\\\\\\\\")
like image 37
Diego Santillan Avatar answered Oct 05 '22 13:10

Diego Santillan