📅  最后修改于: 2020-09-26 05:31:42             🧑  作者: Mango
字符串equalsIgnoreCase()方法中的两个给定的字符串的字符串的内容的基础上而不管比较字符串的情况进行说明。就像equals()方法一样,但不检查大小写。如果任何字符不匹配,则返回false,否则返回true。
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true
: (anotherString != null)
&& (anotherString.value.length == value.length)
&& regionMatches(true, 0, anotherString, 0, value.length);
}
public boolean equalsIgnoreCase(String str)
str:另一个字符串,即与此字符串进行比较。
如果两个字符串的字符相等(忽略大小写),则返回true。
public class EqualsIgnoreCaseExample{
public static void main(String args[]){
String s1=javatpoint;
String s2=javatpoint;
String s3=JAVATPOINT;
String s4=python;
System.out.println(s1.equalsIgnoreCase(s2));//true because content and case both are same
System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored
System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same
}}
true
true
false
让我们看一个示例,在其中测试字符串之间的字符串相等性。
import java.util.ArrayList;
public class EqualsIgnoreCaseExample2 {
public static void main(String[] args) {
String str1 = Mukesh Kumar;
ArrayList list = new ArrayList<>();
list.add(Mohan);
list.add(Mukesh);
list.add(RAVI);
list.add(MuKesH kuMar);
list.add(Suresh);
for (String str : list) {
if (str.equalsIgnoreCase(str1)) {
System.out.println(Mukesh kumar is present);
}
}
}
}
输出:
Mukesh kumar is present