Package org. cric. d;
Public class WhetherIP {
Public static String trimSpaces (String IP) {// remove all spaces before and after the IP String
While (IP. startsWith ("")){
IP = IP. substring (1, IP. length (). trim ();
}
While (IP. endsWith ("")){
IP = IP. substring (0, IP. length ()-1). trim ();
}
Return IP;
}
Public static boolean isIp (String IP) {// determines whether it is an IP address.
Boolean B = false;
IP = trimSpaces (IP );
If (IP. matches ("\ d {1, 3 }\\. \ d {1, 3 }\\. \ d {1, 3 }\\. \ d {1, 3 }")){
String s [] = IP. split ("\\.");
If (Integer. parseInt (s [0]) <255)
If (Integer. parseInt (s [1]) <255)
If (Integer. parseInt (s [2]) <255)
If (Integer. parseInt (s [3]) <255)
B = true;
}
Return B;
}
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
String s = "127.0.0.1 ";
// String s = "23432423 ";
System. out. println (isIp (s )? "Is an IP address": "Not an IP address ");
}
}