当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java String转Boolean用法及代码示例


要将 Java 中的 String 转换为 boolean,可以使用 Boolean.parseBoolean(string)。但如果您想将 String 转换为 Boolean 对象,请使用 Boolean.valueOf(string) 方法。

布尔数据类型仅包含两个值,即真的错误的。如果字符串为 true(忽略大小写),则等效的布尔值将为 true,否则为 false。

Tip: In Java, only true and false are returned as boolean not 0 and 1.

例子:

Input: str = "true"
Output: true
Explanation: The boolean equivalent of true is true itself.
Input: str = "false" 
Output: false 
Explanation: The boolean equivalent of false is false itself.
Input: str = "yes" 
Output: false
Explanation: The boolean equivalent of yes is false since the given value is not equal to true.

1.Using parseBoolean() method of Boolean class

这是将字符串转换为布尔值的最常用方法。此方法用于将给定字符串转换为其原始布尔值。如果给定的字符串包含值真的(忽略大小写),那么这个方法返回真的。如果字符串包含除真的,然后该方法返回错误的.

用法:

boolean boolValue = Boolean.parseBoolean(String str) 

示例

Java


// Java Program to Convert a String to Boolean
// Using parseBoolean() Method of Boolean Class
// Main class
class GFG {
    // Method 1
    // To convert a string to its boolean value
    public static boolean stringToBoolean(String str)
    {
        // Converting a given string to its primitive
        // boolean value using parseBoolean() method
        boolean b1 = Boolean.parseBoolean(str);
        // Return primitive boolean value
        return b1;
    }
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
        // Given String str
        String str = "yes";
        // Printing the desired boolean value
        System.out.println(stringToBoolean(str));
        // Given String str
        str = "true";
        // Printing the desired boolean value
        System.out.println(stringToBoolean(str));
        // Given String str
        str = "false";
        // Printing the desired boolean value
        System.out.println(stringToBoolean(str));
    }
}
输出
false
true
false

2. 使用Boolean类的valueOf()方法

它与上面讨论的方法类似,只是有一点区别,它返回一个布尔对象而不是原始布尔值。

用法:

boolean boolValue = Boolean.valueOf(String str) 

例子:

Java


// Java Program to Convert a String to Boolean Object
// Using valueOf() Method of Boolean Class
// Main class
class GFG {
    // Method 1
    // To convert a string to its boolean object
    public static boolean stringToBoolean(String str)
    {
        // Converting a given string
        // to its boolean object
        // using valueOf() method
        boolean b1 = Boolean.valueOf(str);
        // Returning boolean object
        return b1;
    }
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
        // Given input string 1
        String str = "yes";
        // Printing the desired boolean
        System.out.println(stringToBoolean(str));
        // Given input string 2
        str = "true";
        // Printing the desired boolean
        System.out.println(stringToBoolean(str));
        // Given input string 3
        str = "false";
        // Printing the desired boolean
        System.out.println(stringToBoolean(str));
    }
}
输出
false
true
false


相关用法


注:本文由纯净天空筛选整理自prashant_srivastava大神的英文原创作品 Java Program to Convert String to Boolean。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。