How to convert String object to Boolean Object?

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

How to convert String object to Boolean object?

14条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 02:23

    Use the Apache Commons library BooleanUtils class:

    String[] values= new String[]{"y","Y","n","N","Yes","YES","yes","no","No","NO","true","false","True","False","TRUE","FALSE",null};
    for(String booleanStr : values){
        System.out.println("Str ="+ booleanStr +": boolean =" +BooleanUtils.toBoolean(booleanStr));
    }
    

    Result:

    Str =N: boolean =false
    Str =Yes: boolean =true
    Str =YES: boolean =true
    Str =yes: boolean =true
    Str =no: boolean =false
    Str =No: boolean =false
    Str =NO: boolean =false
    Str =true: boolean =true
    Str =false: boolean =false
    Str =True: boolean =true
    Str =False: boolean =false
    Str =TRUE: boolean =true
    Str =FALSE: boolean =false
    Str =null: boolean =false
    

提交回复
热议问题