📜  Java String equalsIgnoreCase() 方法及示例

📅  最后修改于: 2022-05-13 01:54:29.675000             🧑  作者: Mango

Java String equalsIgnoreCase() 方法及示例

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

句法:

str2.equalsIgnoreCase(str1);

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

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

插图:

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

例子:

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 = true
str2 is equal to str3 = false