String Class

String Compare in Java

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

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


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.

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

String Class - Java Docs

Add to Slack

< String Length   |   String Trim >

© 2019 SyntaxDB. All Rights Reserved.