I have a string which is formed by concatenation of IP addresses, for example:
"127.272.1.43;27.27.1.43;127.127.27.67;128.27.1.43;127.20.1.43;111.27.1.43;127.27.1.43;"
When a new IP address is given, I need to check if the first half of the IP is part of the IP address string. For example, if "127.27.123.23"
is given I need to find if any of the IP address in the string starts with "127.27"
I have the following code, where userIP
= "127.27."
int i = StringUtils.indexOf(dbIPString, userIP);
do {
if (i > 0) {
char ch = dbIPString.charAt(i - 1);
if (ch == ';') {
System.out.println("IP is present in db");
break;
} else {
i = StringUtils.indexOf(dbIPString, userIP, i);
}
} else if (i == 0) {
System.out.println("IP is present in db");
break;
} else {
System.out.println("IP is not present in db");
}
} while (i >= 0);
Can it be more efficient? Or can I use regular expression? Which one is more efficient?