String a = "a.jpg"; String str = a.split(".")[0];
This will throw an ArrayOutOfBoundException because split accepts the arguments regex and "." is a reserved character in a regular expression representing any character. Instead, we should use the following statement:
String str = a.split("\\.")[0];
When the code compiles, the regular expression is called "\.", Which we want it to be
Here is the link of my old blog post if you are interested: http://junxian-huang.blogspot.com/2009/01/java-tip-how-to-split-string-with-dot.html
source share