Method for converting Java string type to boolean type
Today, I accidentally want to convert string type to boolean type. I checked the API document and found that the document seems a little wrong...
Well, just send the test code directly. I'm too lazy to say nonsense...
The above strings are used separately
The returned values of the above five are false
The API documentation says this:
getBoolean public static boolean getBoolean(String name)
Returns true if and only if the system property named after the parameter exists and is equal to the "true" string. (since version 1.0.2 of the JavaTM platform, string testing is no longer case sensitive.) system properties can be accessed through the getproperty method, which is defined by the system class.
Returns false if there is no property named with the specified name or if the specified name is empty or null.
But I don't know why it happened...
Well, the results are all false. What should we do about the conversion? It's okay. There's another method called Boolean parseBoolean(string s);
The API documentation is written as follows:
public static boolean parseBoolean(String s)
Parses a string parameter to a Boolean value. If the string parameter is not null and equals "true" when ignoring case, the returned Boolean represents the true value.
Example: Boolean Parseboolean ("true") returns true.
Example: Boolean Parseboolean ("yes") returns false.
This conversion is OK.... The results are: false, true, false, false
Therefore, it is better to use parseboolean directly during conversion
The above is all about the method of converting Java string type to boolean type brought by Xiaobian. I hope you can support more programming tips~