Home  >  Article  >  Java  >  How to compare strings in java without case sensitivity

How to compare strings in java without case sensitivity

尚
Original
2019-12-30 16:08:575041browse

How to compare strings in java without case sensitivity

In order to perform case-ignoring comparisons in Java, you can call the equalsIgnoreCase() method. When comparing two strings, it will think A-Z and a-z are the same.

Recommended: java video tutorial

equalsIgnoreCase() method is used to compare a string with the specified object, regardless of case.

Syntax

public boolean equalsIgnoreCase(String anotherString)

Parameters

anObject -- The object to compare with the string.

Return value

Returns true if the given object is equal to the string; otherwise returns false.

Example

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 );
    }
}

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of How to compare strings in java without case sensitivity. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn