How to convert String object to Boolean Object?

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

How to convert String object to Boolean object?

14条回答
  •  -上瘾入骨i
    2020-11-28 02:24

    To get the boolean value of a String, try this:

    public boolean toBoolean(String s) {
        try {
            return Boolean.parseBoolean(s); // Successfully converted String to boolean
        } catch(Exception e) {
            return null; // There was some error, so return null.
        }
    }
    

    If there is an error, it will return null. Example:

    toBoolean("true"); // Returns true
    toBoolean("tr.u;e"); // Returns null
    

提交回复
热议问题