I’m embarrassed to ask this one, it’s a real newbie question but it’s been kicking my butt all morning. Here goes:
I’m trying to split an IP address up into four seperate strings using the three periods as delimiters. Here’s the code I am using:
Toast.makeText(getBaseContext(),s,Toast.LENGTH_SHORT).show();
String[] ip = s.split(".",4);
String ip0ne = ip[0];
String ipTwo = ip[1];
String ipThree = ip[2];
String ipFour = ip[3];
‘s’ is the string containing the ip address ‘82.163.99.82’, this is verified in the toast
The problem is, ipOne, ipTwo and ipThree end up containing nothing, and ipFour ends up containing ‘163.99.82’ The first number of the ip address has disappeared altogether. Help please!
The string argument is evaluated as a regular expression and so we have to escape the dot (and in java we have to escape the escape-char too – therefore: a double-backslash)