Java, PHP, CF Code Samples
Converts strings like true, false, yes, no, 1, and 0 to a java boolean variable.
public class Utility {
public static final boolean stringToBoolean(String str) { if (str.equals("0")) { return false; } str = str.toLowerCase(); if (str.equals("false")) { return false; } if (str.equals("no")) { return false; } //if it's non false, it's true by definition return true; }
/* now lets test it */ public static void main(String args[]) { if (args.length > 0) { if (Utility.stringToBoolean(args[0])) { System.out.println(args[0] + " is TRUE"); } else { System.out.println(args[0] + " is FALSE"); } } else { System.out.println("Run the program with a bool to test"); } } }
More Code Samples
|