Home  >  Article  >  Java  >  Use Java's String.equalsIgnoreCase() function to ignore case comparison strings

Use Java's String.equalsIgnoreCase() function to ignore case comparison strings

WBOY
WBOYOriginal
2023-07-25 09:43:462904browse

Use Java's String.equalsIgnoreCase() function to ignore case comparison strings

In Java programming, you often encounter scenarios where you need to compare strings. Sometimes we want to ignore the case of strings for comparison. In this case, we can use the equalsIgnoreCase() function provided by Java's String class.

equalsIgnoreCase() function is a method of the String class, used to compare whether two strings are equal, but will ignore case. This is very commonly used in actual development, especially when the string entered by the user and the preset string may have case differences. Let's take a look at how to use this function.

First, we need to create two strings for comparison:

String str1 = "Hello";
String str2 = "hello";

Next, we can use the equalsIgnoreCase() function to compare whether the two strings are equal:

boolean result = str1.equalsIgnoreCase(str2);

The result of the comparison will be stored in the Boolean variable result. If the two strings are equal, the value of result is true, otherwise it is false.

The following is a complete sample code:

public class StringComparison {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "hello";

        boolean result = str1.equalsIgnoreCase(str2);

        if (result) {
            System.out.println("两个字符串相等");
        } else {
            System.out.println("两个字符串不相等");
        }
    }
}

Run the above code, the output result is:

两个字符串相等

You can see that although str1 and str2 have different case, The results are still equal when compared using the equalsIgnoreCase() function.

It should be noted that this function only compares whether the contents of the string are equal, without considering other characteristics of the string (such as the length of the string, etc.). In addition, the function is case-insensitive, that is, whether the letters in the string are uppercase or lowercase, they are considered equal.

To summarize, using the equalsIgnoreCase() function of Java's String class can conveniently and quickly compare whether two strings are equal while ignoring case. This is very useful in many practical applications, such as when the user name, password, etc. entered by the user are compared with a preset string.

I hope this article will help you understand and use this function!

The above is the detailed content of Use Java's String.equalsIgnoreCase() function to ignore case comparison strings. 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