这里写代码片
import java.util.Arrays;
public class ip_test {
/** * "172.25.27.1 3.25.118.32 105.38.225.12" * 对上面字符串把ip地址切割出来进行排序 * */
public static void main(String[] args) {
String ip = "172.25.27.1 3.25.118.32 105.38.225.12";
System.out.println("原字符串:"+ip);
ip=ip.replaceAll("(\\d+)","00$1");
System.out.println("先补0:"+ip);
ip=ip.replaceAll("0*(\\d{3})","$1");
System.out.println("留三位:"+ip);
String[] new_ip = ip.split(" +");
Arrays.sort(new_ip);
System.out.println("去0再排序后:");
for (String string : new_ip) {
System.out.println(string.replaceAll("0*([0-9]{1,2})","$1"));
}
}
}