在Java中,字符串比較的方法主要有兩種:
示例代碼:
String str1 = "Hello";
String str2 = "hello";
if(str1.equals(str2)) {
System.out.println("兩個(gè)字符串相等");
} else {
System.out.println("兩個(gè)字符串不相等");
}
示例代碼:
String str1 = "apple";
String str2 = "banana";
int result = str1.compareTo(str2);
if(result == 0) {
System.out.println("兩個(gè)字符串相等");
} else if(result < 0) {
System.out.println("str1在str2之前");
} else {
System.out.println("str1在str2之后");
}