Java: Comparing Strings

Last updated on:

Comparison Methods for String Objects:

equals(s1) - Returns true if this string is equal to string s1
equalsIgnoreCase(s1) - Returns true if this string is equal to string s1; case insensitive
compareTo(s1) - Returns an integer greater than 0, equal 0, or less 0 to indicate whether this string is greater than, equal to , or less than s1
startsWith(prefix) - Returns true if this string starting with the specified prefix
endsWith(suffix) - Returns true if this string ending with the specified suffix
contains(s1) - Returns true if s1 is a substring in this string


if (string1.equals(string2))
System.out.print("both strings have same content");
else
System.out.print(strings have different content);

View Comments

Leave a Comment

Use markdown or basic HTML and be nice.