Compare rows on more, less
I can't find in Java
standard mechanisms for checking strings on >
, <
. I'm sure they're there somewhere...
3 answers
Method compareTo()
In Java, compareTo () gets a value of 0 if the argument is a string lexically equal to the given string; a value less than 0 if the argument is a string lexically greater than the string being compared; and a value greater than 0 if the argument is a string lexically smaller this line
public class Test {
public static void main(String args[]) {
String str1 = "Я буду хорошим программистом!";
String str2 = "Я буду хорошим программистом!";
String str3 = "Я буду хорошим дворником!";
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);
}
}
Execution result:
0
11
-11
Use the compareTo method.
Returns 0 if the strings are equal, values less than 0 if the string for which the method was called is less than the string passed in the parameters (on a lexographic basis) and greater than zero-on the contrary.
Depending on the specific circumstances, use String. compareTo, String. compareToIgnoreCase, or Collator. compare.