要对入参IP进行查询,数据库下有起始IP和结束IP两个字段,要查询出入参IP在哪条记录的IP范围内。
String inIp = "10.20.160.2";//入参String startIp = "10.10.160.2";String endIp = "10.50.160.2";要查询出inIp是否在startIp和endIp之间,在范围内就就返回startIp和endIp,不在直接跳出了。
String inIp = "10.20.160.2";//入参String startIp = "10.10.160.2";String endIp = "10.50.160.2";要查询出inIp是否在startIp和endIp之间,在范围内就就返回startIp和endIp,不在直接跳出了。
public class Demo03
{
public static int getValue(String str){
String temp1=str.replace('.',' ');
String[] temp2=temp1.split(" ");
String temp3="";
for(int i=0;i<temp2.length;i++){
temp3+=temp2[i];
}
int temp4=Integer.parseInt(temp3);
return temp4;
} public static void main(String[] args){
String inIp = "10.20.160.2";
String startIp = "10.10.160.2";
String endIp = "10.50.160.2";
int nInIp=getValue(inIp);
int nstartIp=getValue(startIp);
int nEndIp=getValue(endIp);
if(nEndIp>nInIp&&nInIp>nstartIp){
System.out.println("in");
}else{
System.out.println("not in");
}
}
}试试
直接这样
String[] temp2=str.split("\\."); 这样真的可以比较IP吗,总感觉有点不妥
例如:String inIp = "10.20.160.2";//入参
换成整形为 10 * 256 * 256 * 256 + 20 * 256 * 256 + 160 * 256 + 2 = 18784258
之后。比较就可以确定范围了吧!