String Class

String Compare in Java

To compare the contents of a string variable to another string.


Syntax
bool equal = stringName.equals("String literal"); //returns true or false

Notes

The comparison operators cannot be used to check string content, as they are used for the object itself.

equalsIgnoreCase can be used to ignore casing.


Example
String password = "Open Sesame"
if(password.equals("Open Sesame") {
    System.out.println("The door has open.");
} else {
    System.out.println("The door has stayed shut.");
}

See Also
Documentation

String Class - Java Docs

Add to Slack

< String Length   |   String Trim >

© 2019 SyntaxDB. All Rights Reserved.