Java String.CompareTo () method

Source: Internet
Author: User

Java String.CompareTo () method Usage Example Tutorial, this method if the string is an equal argument string then? Returns a value of 0, if the string is less than the string parameter in dictionary order, then returns a value less than 0. If this string is greater than the string argument by dictionary order then a value greater than 0 is described

The Java.lang.String.compareTo () method compares a dictionary of two strings. The comparison is based on the Unicode value of each character in the string. This string object represents a sequence of characters that represents the character sequences that are compared to the dictionary.

    • The result is a negative integer if the argument string in front of this string object dictionary
    • The result is a positive integer if this string object dictionary is the following argument string
    • The result is zero, and if two strings are equal, CompareTo returns 0 o'clock, and the equal (Object) method returns True.
Statement

The following is the declaration Java.lang.String.compareTo () method

public int CompareTo (String anotherstring)
Parameters
    • anotherstring -This is the string used for comparison.

return value

This method returns a value of 0If the string is an equal argument string , if the string is smaller than the string parameter in dictionary order, then returns a value less than 0 if this string is greater than the string argument in dictionary order then A value greater than 0

Abnormal
    • NA

Instance

The following example shows the Java.lang.String.compareTo () method used

Package Com.yiibai;import java.lang.*;p ublic class Stringdemo {public  static void Main (string[] args) {      String s TR1 = "Tutorials", str2 = "point";    Comparing STR1 and str2    int retval = Str1.compareto (str2);    Prints the return value of the comparison    if (retval < 0) {       System.out.println ("STR1 is greater than str2");    }            else if (retval = = 0) {       System.out.println ("str1 is equal to str2");    }            else {       System.out.println ("str1 is less than str2");}}}  

Let's compile and run the above program, which will produce the following results:

STR1 is greater than str2

Java String.CompareTo () method

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.