Compare rows on more, less


I can't find in Java standard mechanisms for checking strings on >, <. I'm sure they're there somewhere...

Author: faoxis, 2016-12-09

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

Example:

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

 6
Author: Ksenia, 2016-12-09 07:55:45

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.

 2
Author: Andrew Bystrov, 2016-12-09 07:56:55

Depending on the specific circumstances, use String. compareTo, String. compareToIgnoreCase, or Collator. compare.

 1
Author: Akina, 2016-12-09 08:16:15