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
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
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