How to convert String object to Boolean Object?

前端 未结 14 837
我寻月下人不归
我寻月下人不归 2020-11-28 01:28

How to convert String object to Boolean object?

14条回答
  •  被撕碎了的回忆
    2020-11-28 02:25

    public static boolean stringToBool(String s) {
            s = s.toLowerCase();
            Set trueSet = new HashSet(Arrays.asList("1", "true", "yes"));
            Set falseSet = new HashSet(Arrays.asList("0", "false", "no"));
    
            if (trueSet.contains(s))
                return true;
            if (falseSet.contains(s))
                return false;
    
            throw new IllegalArgumentException(s + " is not a boolean.");
        }
    

    My way to convert string to boolean.

提交回复
热议问题