小编Bjo*_*cke的帖子

Java:检查字符串是否只包含1,0或一个点

我正在尝试检查字符串是否仅包含

  • 1

  • 0

  • .

或这三者的组合.

首先我有这个代码:

public static boolean controleSubnetmask(String mask) {
    try {
        String[] maskArray = mask.split(".");
        int[] subnetmask = new int[4];

        //array of string to array of int
        for (int i = 0; i < maskArray.length; i++) {
            subnetmask[i] = Integer.parseInt(maskArray[i]);
        }
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是它的功能相当复杂,并且它不会检查是否只输入1和0.所以现在我有了这个,但似乎我误解了正则表达式,因为它不起作用:

public static void controleSubnetmask(String mask) {
    mask = "1100.110...11";
    String test = "p";
    if (mask.contains("[^10\\.]") == true) {
        System.out.println("wrong input");
    }
    if (test.contains("[^10\\.]") == …
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
2917
查看次数

标签 统计

java ×1