當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。