在Java中,可以使用equals()方法來比較兩個字符串是否相等。示例如下:
String str1 = "Hello";
String str2 = "World";
if(str1.equals(str2)) {
System.out.println("The two strings are equal.");
} else {
System.out.println("The two strings are not equal.");
}
另外,還可以使用equalsIgnoreCase()方法來忽略大小寫比較兩個字符串是否相等。示例如下:
String str1 = "hello";
String str2 = "HELLO";
if(str1.equalsIgnoreCase(str2)) {
System.out.println("The two strings are equal ignoring case.");
} else {
System.out.println("The two strings are not equal ignoring case.");
}