Comparison of strings in Java between equals () and equalsignorecase ()

Source: Internet
Author: User

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

Boolean equals (Object Str)

Here, STR is a string object used to compare with the call String object. If the two strings have the same characters and length, true is returned; otherwise, false is returned. This comparison is case sensitive.

2. To perform case-insensitive comparison, you can call the equalsignorecase () method. When comparing two strings, it considers the A-Z and A-Z are the same. The general form is as follows:

Boolean inclusignorecase (string Str)

Here, STR is a string object used to compare with the call String object. If the two strings have the same characters and length, true is returned. Otherwise, false is returned. The following example illustrates the equals () and equalsignorecase () methods:

// Demonstrate equals () and inclusignorecase ().

Class sdemo {

Public static void main (string ARGs []) {

String S1 = "hello ";

String S2 = "hello ";

String S3 = "good-bye ";

String S4 = "hello ";

System. Out. println (S1 + "equals" + S2 + "->" +

S1.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 program output is as follows:

Hello equals hello-> true

Hello equals good-bye-> false

Hello equals hello-> false

Hello equalsignorecase hello-> true

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.