• Post Reply Bookmark Topic Watch Topic
  • New Topic

replaceAll method of String class  RSS feed

 
S Babs
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have following code
import java.io.*
public class ReplaceString {
public static void main(String[] args){
String sURL = "/server/default/lib":
sURL = sURL.replaceAll("/",File.separator);
System.out.println("Replace string: "+sURL);
}
}
The above code works fine in Linux for the version of j2sdk1.4.0 (linux version)
and the same code is giving the following exception
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:455)
at java.util.regex.Matcher.appendReplacement(Matcher.java:532)
at java.util.regex.Matcher.replaceAll(Matcher.java:636)
at java.lang.String.replaceAll(String.java:1706)
at ReplaceString.main(ReplaceString.java:6)
with the same version of j2sdk1.4.0 (windows version)
Whats the funda here?
Please get me out of this..
very urgent
thanks in advance
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming the Linux file separator is "/" or some other valid regex. In windows, File.separator returns "\\" which is only a valid regex after you escape it with like "\\\\".
Jamie
[ July 30, 2003: Message edited by: Jamie Robertson ]
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gee, if I can challenge a Bartender here :roll:
I'm assuming the Linux file separator is "/" or some other valid regex.
From the API: File.separator The system-dependent default name-separator character, represented as a string for convenience.
File.separator returns "\\" which is only a valid regex after you escape it with like "\\\\".
The signature for ReplaceAll is:
public String replaceAll(String regex, String replacement)
The replacement is just a simple string. Admittedly, the reason we need all these backslashes (which can result in
LTS) is still the same and that is due to the multiple processing the Regex replacement engine must do on the String.
A good platform independent solution could be:

I use the File.separatorChar which is just a simple Char preceded by a backslash.
For Windows the resultant String passed is "\\\\".
For Linux the resultant String passed is "\\/".
Since there is nothing special of the '\/' sequence it is simply ignored and taken as '/'.
One more thing .... I tested it and it works
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leslie Chaim:
The replacement is just a simple string.

No, it isn't. From http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Matcher.html#replaceAll (which String#replaceAll delegates to):
"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."
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we're going to do nitpicking let's do it all the way
Yes, you're right! Thanks for the correction, Ilja.
I really meant that the replacement String is NOT a regex.
BTW, and this is a little off the topic, how would you check for the OS type if Windows|Linux?
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my defense, my response stated that in linux, the file separator that is returned is a valid regular expression, thus no problems on linux. Windows returns a file separator that is not a valid regex, so it needs to be escaped.
This would be what I was elluding to:

Jamie
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leslie Chaim:
BTW, and this is a little off the topic, how would you check for the OS type if Windows|Linux?

System.getProperty("os.name")
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!