How to convert String object to Boolean Object?

前端 未结 14 653
我寻月下人不归
我寻月下人不归 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<String> trueSet = new HashSet<String>(Arrays.asList("1", "true", "yes"));
            Set<String> falseSet = new HashSet<String>(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.

    0 讨论(0)
  • 2020-11-28 02:26

    This is how I did it:

    "1##true".contains( string )

    For my case is mostly either 1 or true. I use hashes as dividers.

    0 讨论(0)
提交回复
热议问题