本文概述
- Java字符串equalsIgnoreCase()方法示例
- Java字符串equalsIgnoreCase()方法示例2
内部实施
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。
Java字符串equalsIgnoreCase()方法示例
public class EqualsIgnoreCaseExample{
public static void main(String args[]){
String s1="srcmini";
String s2="srcmini";
String s3="srcmini";
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
}}
【Java字符串equalsIgnoreCase()】立即测试
true
true
false
Java字符串equalsIgnoreCase()方法示例2让我们看一个示例, 在其中测试字符串之间的字符串相等性。
import java.util.ArrayList;
public class EqualsIgnoreCaseExample2 {
public static void main(String[] args) {
String str1 = "Mukesh Kumar";
ArrayList<
String>
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
推荐阅读
- Java字符串format()
- Java字符串常见问题解答或面试问题
- Java字符串equals()
- Java字符串contains()
- Java字符串endsWith()
- Java字符串连接
- Java字符串介绍
- Java字符串compareTo()
- Java字符串charAt()