Question:
How do you compare two string in java?
Code:
public class CompareStrings { public static void main(String[] args) { String str1 = "foo"; String str2 = "foo"; String str3 = "FOO"; System.out.println("str1: " + str1); System.out.println("str2: " + str2); // java String comparison with the equals method if ( str1.equals(str2) ){ System.out.println("str1 and str2 are the same"); } // java String comparison with the equalsIgnoreCase method if ( str1.equalsIgnoreCase(str3)) { System.out.println("str1 and str3 match ignooring case"); } // java String comparison with the compareTo method if ( str1.compareTo(str2) == 0) { System.out.println("str1 and str2 are the same"); } // java String comparison with the compareToIgnoreCase method if ( str1.compareToIgnoreCase(str3) == 0) { System.out.println("str1 and str3 match ignooring case"); } } }
Output:
$ java CompareStrings str1: foo str2: foo str1 and str2 are the same str1 and str3 match ignooring case str1 and str2 are the same str1 and str3 match ignooring case