String.split returning null when using a dot

Viewed 6408

I got this simple code:

String ip = "1.2.3.4";
String[] ipArray = ip.split(".");
System.out.println(ipArray[1]);

And ipArray is null by the time it hits System.out.println (throws null pointer exception).

My question is why does ipArray stay null even though I'm setting it to split on each of ip's .s?

4 Answers

We can also use split ("[.]")

String s = "abcd.jpg";
String[] strarry = s.split ("[.]");

strarry[0]=abcd, strarry[1]=jpg

Related