String s = "java.awt.Color.r.0.g.255.b.0.";
String[] split = s.split(".");
System.out.println(split[0]);
Problem with splitting
Started by Sinipull, Mar 28 2010 02:23 AM
3 replies to this topic
#1
Posted 28 March 2010 - 02:23 AM
Why doesn't this code work? Throws ArrayIndexOutOfBoundsException. Really weird, what am i missing?
|
|
|
#2
Posted 28 March 2010 - 05:23 AM
It happens because String.Split() expects a regular expression pattern string as a parameter, And in regular expressions, the "." is a special character, so to tell regular expressions that you want to match the exact "." you just add an escape character so it looks like "\." But still won't work because you need another escape character for java (1 for regex and 1 for java string) so all you need is to replace s.split(".") with s.split("\\.").
String s = "java.awt.Color.r.0.g.255.b.0.";
String[] split = s.split("\\."); //regex delimiter
System.out.println(split[0]);
#3
Posted 28 March 2010 - 05:45 AM
Thanks a lot, for explaining :)
#4
Posted 29 March 2010 - 01:15 PM
No problem. =)


Sign In
Create Account


Back to top









