Android: String.split() method not actually splitting?

Maybe it’s been a long day and I’ve overlooked something, but this is driving me absolutely crazy.

String[] address = "192.169.2.10".split(".");
Log.d(TAG, "Address array length: "+address.length); 
//Output: 'Address array length: 0'

I am resolving the device IP address, and trying to split it into an String array by splitting it on the period characters. Obviously this should return an array with a length of 4, but it’s returning a length of 0.

Is there something blatantly obvious I am overlooking here?

NOTE: The real code is pulling that IP address in from WifiManager, but even if I put an arbitrary IP String in there like above, the length is 0.

Answer

The argument to split is a regular expression, and the . has a special meaning, so you’ll need to escape it:

String[] address = "192.169.2.10".split("\.");

Leave a Reply

Your email address will not be published. Required fields are marked *