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


I have to change \\ with \ in Java. The code I'm using

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

But I do not know why this is throwing StringIndexOutOfBoundsException .

It says outside the string index range: 1

What could be the reason for this? I think this is the first argument because replaced all a pattern accepts what could be possible solutions?


Stacktrace

  Exception in thread "main" java.lang.StringIndexOutOfBoundsException: string index out Category: 1 on java.lang .String.charAt (String.java.This) at java.util.regex.Matcher.appendReplacement (Matcher.java:696) at java.util.regex.Matcher.replaceAll (Matcher.java): 806) java.lang. On String.replaceAll (String.Java Month 2000)  

got the answer

The code is required, but I do not know why he removed it here in any case.

One has already been filed in the Java Bug Database. (Thanks for this reference, asalamon.)

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

Surprisingly, the search and the location of the string are identical both :) But still it is necessary for me.

To avoid this using a code, replaceAll instead of Use String.replace :

  string origin = MyConstants.LOCATION_PATH + file. Separator + myObject.getStLocation (); System.out.println (original location ("\\", "\\"));  

Personally I will not do this like this - I have a MyConstants.LOCATION_PATH_FILE Can be created as a file and then you can type:

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

Which will do the right thing correctly.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -