如何在 Java 中比較字串等價性?
您可以使用 equals() 方法檢查 Java 中兩個字串的相等性。此方法將此字串與指定物件進行比較。結果僅在引數不為 null 且是表示與此物件相同的字元序列的字串物件時為真。
示例
import java.lang.*
public class StringDemo {
public static void main(String[] args) {
String str1 = "Tutorialspoint";
String str2 = "Tutorialspoint";
String str3 = "Hi";
// checking for equality
boolean retval1 = str2.equals(str1);
boolean retval2 = str2.equals(str3);
// prints the return value
System.out.println("str2 is equal to str1 = " + retval1);
System.out.println("str2 is equal to str3 = " + retval2);
}
}輸出
str2 is equal to str1 = true str2 is equal to str3 = false
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP