Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split from full stop in java [duplicate]

Tags:

java

Possible Duplicate:
The split() method in Java does not work on a dot (.)

I'm new to java. I want to split a String from "." (dot) and get those names one by one. But this program gives error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0" please help me

 String input1 = "van.bus.car";

 System.out.println(input.split(".")[0]+"");

 System.out.println(input.split(".")[1]+"");

 System.out.println(input.split(".")[2]+"");
like image 495
heshan Avatar asked Jul 09 '26 20:07

heshan


1 Answers

In regex, Dot(.) is a special meta-character which matches everything.

Since String.split works on Regex, so you need to escape it with backslash if you want to match a dot.

System.out.println(input.split("\\.")[0]+"");

To learn more about Regex, refer to following sites: -

  • http://docs.oracle.com/javase/tutorial/essential/regex/
  • http://www.vogella.com/articles/JavaRegularExpressions/article.html
  • http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
like image 117
Rohit Jain Avatar answered Jul 12 '26 08:07

Rohit Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!