如何在 Java 中比较两个字符串并忽略大小写
equalsIgnoreCase() 方法将该字符串与指定对象进行比较。如果且仅当参数不为 null 并且是表示与该对象相同的字符序列的 String 对象时,结果为 true。
示例
public class Sample { public static void main(String args[]) { String Str1 = new String("This is really not immutable!!"); String Str2 = Str1; String Str3 = new String("THIS IS REALLY NOT IMMUTABLE!!"); boolean retVal; retVal = Str1.equalsIgnoreCase(Str2); System.out.println("Returned Value = " + retVal ); retVal = Str1.equalsIgnoreCase( Str3 ); System.out.println("Returned Value = " + retVal ); } }
输出
Returned Value = true Returned Value = true
广告