Usage of CompareTo in Java

Source: Internet
Author: User

the CompareTo method in Java, returns the difference between the ASC codes of the previous two strings participating in the comparison, and the following set of codeString a= "A", b= "B";System.out.println (A.COMPARETO.B);then output-1;if A= "a", b= "a" is output 0;if a= "B", the b= "a" is output 1; when a single character is compared, what if the string is longer?? if a= "AB", b= "B", then output-1;if A= "abcdef", b= "B" is output-1;That is , if the first letter of the two string is different, the method returns the difference of the first letter of the ASC code; What if the first letter is the same?? if a= "AB", b= "a", Output 1;if A= "abcdef", b= "a" output 5;if A= "abcdef", b= "abc" Output 3;if A= "abcdef", b= "Ace" output-1; that is, the two strings participating in the comparison, if the first character is the same, the next character is compared until there are different, the ASC code difference of the different characters is returned, if the two strings are not the same length, the characters that can participate in the comparison are identical, the length difference of two strings is returned.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Usage of CompareTo in Java

Related Article

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.