判断字符串是否是IP
package my; public class BooleanIP { boolean isIpLegal(String string){ if(string ==null ||string.length() < 7 || string.length() > 15){ return false; } if(string.charAt(0) == '.' || string.charAt(string.length()-1) == '.'){ return false; } String arr[] = new String[4]; arr =string.split("\."); if(arr.length > 4){ return false; } for(int i=0;i<arr.length;i++){ if(arr[i].charAt(0) == '0'){ return false; } for(int j=0;j<arr[i].length();j++){ if(arr[i].charAt(j) <'0' || arr[i].charAt(j)>'9'){ return false; } } } int a1= Integer.parseInt(arr[0]); int a2=Integer.parseInt(arr[1]); int a3=Integer.parseInt(arr[2]); int a4=Integer.parseInt(arr[3]); if(a1 >= 1&& a1 <=255 && a2>=0&& a2 <= 255&& a3>=0 &&a3<=255 &&a4>=0 && a4<=255){ return true; }else{ return false; } } public static void main(String[] args){ String str ="185.192.255.255"; BooleanIP ip= new BooleanIP(); boolean isIp = ip.isIpLegal(str); System.out.println(isIp); } }