Java String type converts a Boolean type method _java

Source: Internet
Author: User
Tags documentation

Today, I accidentally tried to convert the string type to a Boolean type, looked up the API documentation, and found that the document seemed a bit out of place ...

Well, just send the test code, nonsense too lazy to say ...

String S1 = "false"; 
String s2 = "true"; 
String s3 = "FAlSe"; 
String S4 = "TrUe"; 
String S5 = "True_a"; 

The above string is separated by

Boolean.getboolean (S1); 
Boolean.getboolean (S2) 
Boolean.getboolean (S3); 
Boolean.getboolean (S4); 
Boolean.getboolean (S5); 

All of the above 5 returned values are false

This is what the API documentation says:

Getboolean
public static Boolean Getboolean (String name)

Returns true if and only if the system attribute named with the parameter exists and is equal to the "true" string. (Starting with the 1.0.2 version of the JAVATM platform, the test for strings is no longer case-sensitive.) You can access system properties through the GetProperty method, which is defined by the system class.

Returns False if no property is named with the specified name or if the specified name is null or NULL.

But it turns out I don't know why.

Well, it turns out to be false, so what are we going to do, okay, there's another method called Boolean.parseboolean (string s);


Boolean.parseboolean (S1); 
Boolean.parseboolean (S2) 
Boolean.parseboolean (S3); 
Boolean.parseboolean (S4); 
Boolean.parseboolean (S5); 

The API documentation is written like this:

public static Boolean Parseboolean (String s)

Resolves a string parameter to a boolean value. If the String argument is not null and equals "true" when the case is ignored, the returned Boolean represents a true value.

Example: Boolean.parseboolean ("true") returns TRUE.

Example: Boolean.parseboolean ("yes") returns false.

This time the conversion will be OK .... The results are: false, True, False, True, False

So, when you convert, just use parseboolean .

The above is the small series for everyone to bring the Java String type conversion Boolean type of method all the content, I hope that we support cloud Habitat Community ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.