import java.util.Arrays;
public class ip_test {
/** * "172.25.27.1 3.25.118.32 105.38.225.12" * 对上面字符串把ip地址切割出来进行排序 , 如果直接排序,得出的结果是105.38.225.12
172.25.27.1 3.25.118.32 不符合要求 * */
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( " +");
// System.out.println(new_ip);//这样打印的是字符串数组的首地址
Arrays.sort(new_ip);
System.out.println( "去0再排序后:");
for (String string : new_ip) {
System.out.println(string.replaceAll( "0*([0-9]{1,2})", "$1"));
}
}
}