Java字符串compareToIgnoreCase()方法与示例

字符串compareToIgnoreCase()方法 (String compareToIgnoreCase() Method)

compareToIgnoreCase() is a String method in Java and it is used to compare two strings by ignoring the case's sensitivity.

compareToIgnoreCase()是Java中的String方法,它通过忽略大小写的敏感性来比较两个字符串。

If both strings are equal (without considering the case-sensitivity) – it returns 0, otherwise, it returns a value less than 0 or greater than 0 based on the first dissimilar characters difference.

如果两个字符串相等(不考虑大小写敏感性),则返回0 ,否则,根据第一个不同的字符差异返回小于0大于0的值。

Syntax:

句法:

    int string1.compareToIgnoreCase(string2);

Here, string1 and string2 are the strings to be compared, and it returns an integer value that is 0, less than 0 or greater than 0.

在这里, string1和string2是要比较的字符串,它返回一个整数值,该值是0,小于0或大于0。

Example:

例:

    Input: 
    str1 = "Hello world!"
    str2 = "Hello world!"

    Output:
    0

    Input: 
    str1 = "Hello world!"
    str2 = "HELLO WORLD!"

    Output:
    0

    Input: 
    str1 = "Hello world!"
    str2 = "Hi guys!!!"

    Output:
    -4

Java code to compare strings using String.compareToIgnoreCase() method

Java代码使用String.compareToIgnoreCase()方法比较字符串

public class Main
{
    public static void main(String[] args) {
        String str1 = "Hello world!";
        String str2 = "Hello world!";
        String str3 = "HELLO WORLD!";
        
        System.out.println("str1 and str2 = " + str1.compareToIgnoreCase(str2));
        System.out.println("str1 and str3 = " + str1.compareToIgnoreCase(str3));
        System.out.println("str2 and str3 = " + str2.compareToIgnoreCase(str3));
        
        //checking with the condition
        if(str1.compareToIgnoreCase(str2)==0){
            System.out.println("str1 is equal to str2");
        }
        else{
            System.out.println("str1 is not equal to str2");
        }

        if(str1.compareToIgnoreCase(str3)==0){
            System.out.println("str1 is equal to str3");
        }
        else{
            System.out.println("str1 is not equal to str3");
        }        
        
        if(str2.compareToIgnoreCase(str3)==0){
            System.out.println("str2 is equal to str3");
        }
        else{
            System.out.println("str2 is not equal to str3");
        }        
        
    }
}

Output

输出量

str1.compareToIgnoreCase(str2) = 0
str1.compareToIgnoreCase(str3) = 0
str2.compareToIgnoreCase(str3) = 0
str1 is equal to str2
str1 is equal to str3
str2 is equal to str3

public class Main
{
    public static void main(String[] args) {
        String str1 = "Hello world!";
        String str2 = "Hi guys!!!";
        
        if(str1.compareToIgnoreCase(str2)==0){
            System.out.println("str1 is equal to str2");
        }
        else{
            System.out.println("str1 is not equal to str2");
        }        
    }
}

Output

输出量

str1 is not equal to str2


翻译自: https://www.includehelp.com/java/string-compareToIgnoreCase-method-with-example.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值