String boolean value (ex:True or False) to convert to Boolean - Java java.lang

Java examples for java.lang:boolean

Description

String boolean value (ex:True or False) to convert to Boolean

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String obj = "java2s.com";
        System.out.println(getBoolean(obj));
    }//from  www  .j  a v a 2  s .  c o  m

    /**
     * String boolean value (ex:True or False) to convert to Boolean
     * @param obj
     * @return
     */
    public static Boolean getBoolean(String obj) {
        if (obj == null || nullToVoid(obj).equals("")) {
            return false;
        } else {
            return Boolean.valueOf(obj);
        }
    }

    /**
     * if String is null, set an empty string
     * @param obj
     * @return
     */
    public static String nullToVoid(Object obj) {
        if (obj == null) {
            return "";
        } else {
            return String.valueOf(obj).trim();
        }
    }

    /**
     * if String is null, return a defVal string
     * @param obj
     * @param defVal
     * @return
     */
    public static String nullToVoid(Object obj, String defVal) {
        if (obj == null) {
            return defVal;
        } else {
            return String.valueOf(obj).trim();
        }
    }
}

Related Tutorials