如何在 Java 中检查两个字符串是否相等?
你可以使用 Java 中的 equals() 方法检查两个字符串是否相等。此方法将此字符串与指定对象进行比较。仅当参数不为 null,并且是一个表示与该对象相同的字符序列的 String 对象时,结果才为 true。
示例
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
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP