Language
Version: SE 8
Categories
-
User Program Communication
-
Variables
-
Control Flow
-
Object Oriented Programming
-
String Class
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.");
}