如何在 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