Java String equalsIgnoreCase()方法及其示例

这个 equalsIgnoreCase()方法 关于 字符串类 比较两个字符串,而不考虑字符串的大小写(小写或大写)。此方法返回一个布尔值,如果参数不为null,则返回true,表示忽略大小写的等效字符串,否则返回false。

null

语法:

str2.equalsIgnoreCase(str1);

注: 这里str1和str2都是我们需要比较的字符串。

参数: 应该被比较的字符串。

返回类型: 一个布尔值,如果参数不为null且表示忽略大小写的等效字符串,则为true,否则为false。

插图:

Input : str1 = "pAwAn";        str2 = "PAWan"        str2.equalsIgnoreCase(str1);Output :true
Input : str1 = "powAn";        str2 = "PAWan"        str2.equalsIgnoreCase(str1);Output :falseExplanation: powan and pawan are different strings. 

注: str1是一个字符串,需要与str2进行比较。

例子:

JAVA

// Java Program to Illustrate equalsIgnoreCase() method
// Importing required classes
import java.lang.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing strings to compare
String str1 = "GeeKS FOr gEEks" ;
String str2 = "geeKs foR gEEKs" ;
String str3 = "ksgee orF geeks" ;
// Comparing strings
// If we ignore the cases
boolean result1 = str2.equalsIgnoreCase(str1);
// Both the strings are equal so display true
System.out.println( "str2 is equal to str1 = "
+ result1);
// Even if we ignore the cases
boolean result2 = str2.equalsIgnoreCase(str3);
// Both the strings are not equal so display false
System.out.println( "str2 is equal to str3 = "
+ result2);
}
}


输出

str2 is equal to str1 = truestr2 is equal to str3 = false

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享