java比较字符不区分大小写的方法

爱蒂网

java比较字符不区分大小写的方法

2020-04-10 12:30:21 分类 / 网络编程 来源 / 互联网

java比较字符不区分大小写的方法:java中使用​equalsIgnoreCase()即可将字符串与指定的对象比较,不考虑大小写。如果给定对象与字符串相等,则返回true;否则返回false。

equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。(推荐:java视频教程)

语法

public boolean equalsIgnoreCase(String anotherString)

参数:anObject -- 与字符串进行比较的对象。

返回值:如果给定对象与字符串相等,则返回 true;否则返回 false。

实例

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("runoob");
        String Str2 = Str1;
        String Str3 = new String("runoob");
        String Str4 = new String("RUNOOB");
        boolean retVal;

        retVal = Str1.equals( Str2 );
        System.out.println("返回值 = " + retVal );

        retVal = Str3.equals( Str4);
        System.out.println("返回值 = " + retVal );

        retVal = Str1.equalsIgnoreCase( Str4 );
        System.out.println("返回值 = " + retVal );
    }
}

以上程序执行结果为:

返回值 = true
返回值 = false
返回值 = true

更多java知识请关注java基础教程栏目。

以上就是java比较字符不区分大小写的方法的详细内容,更多请关注爱蒂网其它相关文章!

猜你喜欢