该功能应从IPv4地址中删除前导零。地址007.006.02.90例如应转换为7.6.2.90。似乎问题是此行:

public static String zero_cutter(String ip) {
    String ipnew;
    String []  spliti = ip.split(".");
    int[] myArray = new int[4];

    for (int i = 0; i < 4; i++) {
        myArray [i] = Integer.parseInt(spliti [i]);
    }

    for (int i = 0; i < 4; i++) {
        spliti [i]  = Integer.toString(myArray [i]);
    }
    ipnew = spliti [0] + "." + spliti [1] + "." + spliti [2] +    "." + spliti [3];

    return ipnew;
}

最佳答案

您必须在模式中转义.字符,因为点是与任何字符匹配的特殊字符(根据java.util.regex.Pattern javadoc)。


  预定义字符类
  
  。任何字符(可能或可能不匹配行终止符)


一种方法是在模式中使用\\

String[] spliti = ip.split("\\.");\


但是,您可以将代码替换为InetAddress类:

String ip = "007.006.002.090";
InetAddress addr = InetAddress.getByName(ip);
System.out.println(addr.getHostAddress()); // 7.6.2.90

07-24 09:16