Home >Java >javaTutorial >Share the differences between equals and equalsignorecase in Java and tutorials on how to use them

Share the differences between equals and equalsignorecase in Java and tutorials on how to use them

零下一度
零下一度Original
2017-05-23 10:49:463421browse

The difference and use of equals and equalsignorecase in Java, the editor will take you to look at it in detail

When comparing equals, you need to compare whether the case is the same, and the latter is ignored Upper and lower case, ignore means to ignore. For example, abc and Abc, the former returns false and the latter returns true

1. Use the equals() method to compare whether two strings are equal. It has the following general form:

boolean equals(Object str)

Where str is a String object used to compare with the calling String (String) object. It returns true if two strings have the same characters and length, otherwise it returns false. This comparison is case-sensitive.

2. In order to perform comparisons that ignore case, you can call the equalsIgnoreCase() method. When comparing two strings, it will think A-Z and a-z are the same. Its general form is as follows:

boolean equalsIgnoreCase(String str)

Here, str is a String object used to compare with the calling String object. It also returns true if the two strings have the same characters and length, false otherwise.

The following example illustrates the equals() and equalsIgnoreCase() methods:

class equalsDemo {
public static void main(String args[]) {
String <a href="https://www.baidu.com/s?wd=s1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y1rj0vrj03rHRYrH9-ryD30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnW0Ln1mkPjbsP1R3PWDznWcvr0" target="_blank" class="baidu-highlight">s1</a> = "Hello";
String s2 = "Hello";
String s3 = "Good-bye";
String s4 = "HELLO";
System.out.println(<a href="https://www.baidu.com/s?wd=s1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y1rj0vrj03rHRYrH9-ryD30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnW0Ln1mkPjbsP1R3PWDznWcvr0"target="_blank" class="baidu-highlight">s1</a> + " equals " + s2 + " -> " +
<a href="https://www.baidu.com/s?wd=s1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y1rj0vrj03rHRYrH9-ryD30ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnW0Ln1mkPjbsP1R3PWDznWcvr0" target="_blank" class="baidu-highlight">s1</a>.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " +
s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " +
s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " +
s1.equalsIgnoreCase(s4));
}
}

The output of this program is as follows:

Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true

[Related recommendations]

1. Detailed explanation of the differences between equals(), equalsIgnoreCase() and == in Java

2. Introduction to Java equalsIgnoreCase() method examples

3. Image and text examples of equalsIgnoreCase method in java

The above is the detailed content of Share the differences between equals and equalsignorecase in Java and tutorials on how to use them. 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