当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java String equalsIgnoreCase()用法及代码示例

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

equalsIgnoreCase() 的语法

str2.equalsIgnoreCase(str1);

参数

  • 字符串1:应该进行比较的字符串。

返回值

  • 一个布尔值,如果参数不为 null 并且表示等效的 String 忽略大小写,则为 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. 

Note: str1 is a string that needs to be compared with str2.

内部实施equalsIgnoreCase()方法

public boolean equalsIgnoreCase(String str) 
{    
       return (this == str) ? true    
               : (str != null)
               && (str.value.length == value.length)    
               && regionMatches(true, 0, str, 0, value.length);
}    

字符串equalsIgnoreCase()方法示例

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

相关用法


注:本文由纯净天空筛选整理自pawan_asipu大神的英文原创作品 Java String equalsIgnoreCase() Method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。