Comparing Strings and Portions of Strings in Java


Following is an example which compares Strings and portion of strings in Java? Explain with an example.

Example

Live Demo

public class StringDemo {
   public static void main(String[] args) {
      String str1 = "tutorials point";
      String str2 = str1.substring(10);
      int result = str1.compareTo(str2);

      // prints the return value of the comparison
      if (result < 0) {
         System.out.println("str1 is greater than str2");
      } else if (result == 0) {
         System.out.println("str1 is equal to str2");
      } else {
         System.out.println("str1 is less than str2");
      }
   }
}

Output

str1 is less than str2

Updated on: 26-Feb-2020

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements