你的替换操作是对整个字符串进行操作的,并没有进行分割操作并分别打印操作,打出来的当然还是整个字符串
public static void ipSort()
{
String ip = "192.68.1.254 102.49.23.013 10.10.10.10 2.2.2.2 8.109.90.30";
ip = ip.replaceAll("(\\d+)","00$1");
System.out.println(ip);
ip = ip.replaceAll("0*(\\d{3})","$1");
System.out.println(ip);
String[] arr = ip.split(" ");
TreeSet<String> ts = new TreeSet<String>();
//这里也可以用Array.sort(arr[])进行升序排序,如果有相同对象的话就不好用TreeSet了
for(String s : arr)
{
ts.add(s);
}
for(String s : ts)
{
System.out.println(s.replaceAll("0*(\\d+)","$1"));
}
} |