tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Commons Lang3 > Booleans > String to Boolean

String to Boolean 

Apache Commons Lang 3.0 is a java library with lot of utilities and reusable components. This requires the library commons-lang3-3.0.1.jar to be in classpath. The following example shows converting string to boolean value.

File Name  :  
com/bethecoder/tutorials/commons_lang/tests/booleans/ToBooleanObjectTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.commons_lang.tests.booleans;

import org.apache.commons.lang3.BooleanUtils;

public class ToBooleanObjectTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    System.out.println(BooleanUtils.toBoolean((String)null));
    System.out.println(BooleanUtils.toBoolean("true"));
    System.out.println(BooleanUtils.toBoolean("false"));
    
    System.out.println(BooleanUtils.toBoolean("TruE"));
    System.out.println(BooleanUtils.toBoolean("FalsE"));
    
    System.out.println(BooleanUtils.toBoolean("on"));
    System.out.println(BooleanUtils.toBoolean("ON"));
    
    System.out.println(BooleanUtils.toBoolean("off"));
    System.out.println(BooleanUtils.toBoolean("OFF"));
    
    System.out.println(BooleanUtils.toBoolean("abc"));
    System.out.println(BooleanUtils.toBoolean("1234"));
  }

}
   

It gives the following output,
false
true
false

true
false

true
true

false
false

false
false



 
  


  
bl  br