📌  相关文章
📜  Java程序将字符串转换为布尔值

📅  最后修改于: 2022-05-13 01:55:32.700000             🧑  作者: Mango

Java程序将字符串转换为布尔值

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

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

例子:

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.使用Boolean类的parseBoolean()方法

这是将字符串转换为布尔值的最常用方法。此方法用于将给定字符串转换为其原始布尔值。如果给定的字符串包含值true (忽略大小写),则此方法返回true 。如果字符串包含除true之外的任何其他值,则该方法返回false

句法:

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));
    }
}


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

2.使用布尔类的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