The compareTo() method is used to compare the two approaches:
String is compared to an object.
compares two strings in dictionary order.
syntax
int compareTo(Object o)
or
Int compareTo(String anotherString)
parameter
o – object to compare.
AnotherString – The string to compare.
Return value
the return value is the integer, it is first to compare the size of the corresponding character (ASCII), if the first character and the parameters of the first character, end of comparison, return the difference between them, if the first character is equal to the parameters of the first character, with the second character and parameter of the second character, and so on, until the comparison of character or character one end are being compared.
If the parameter string is equal to this string, the value 0 is returned;
returns a value less than 0 if the string is smaller than the string argument;
returns a value greater than 0 if the string is greater than the string argument.
instance
public class Test {
public static void main(String args[]) {
String str1 = "Strings";
String str2 = "Strings";
String str3 = "Strings123";
int result = str1.compareTo( str2 );
System.out.println(result);
result = str2.compareTo( str3 );
System.out.println(result);
result = str3.compareTo( str1 );
System.out.println(result);
}
}
above program execution result is:
0
-3
3
Read More:
- The method of Java jumping out of loop
- On the intern () method of string class in Java
- Java and Android commons codec usage and nosuch method error
- java.lang.IllegalStateException: Could not execute method for android:onClick
- Java uses the createnewfile() method to report an error
- Difference between isempty method and isblank method in stringutils
- Error java.lang.IllegalArgumentException: Validation failed for query for method public abstract
- How to generate main () method in java graphical interface
- Method threw ‘java.lang.NullPointerException‘ exception. Cannot evaluate com.sun.proxy.xxx
- Spock + powermock simulation static method reports an error java.lang.classcastexception
- Caused by: java.lang.IllegalStateException: Ambiguou There is already ‘XXXXXXController‘ bean method
- Apache Groovy——java.lang.NoSuchMethodError: x.x.x: method <init>()V not found
- “Method does not override method from its superclass”
- The differences between the equals method in the string class and the equals method in the object class
- Override the equals method and override the toString method
- Solve the problem that power view in Excel prompts activate method of oleobject class failed or activate method like oleobject is invalid
- Error in idea compilation: java.lang.OutOfMemoryError Java heap space and java.lang.StackOverflowError
- java.lang.NoSuchMethodError: org.json.JSONObject.put(Java/lang/String; Java/util/Collection;) “209151;”
- Python’s direct method for solving linear equations (5) — square root method for solving linear equations
- Detailed explanation of Python__ new__() method