String: compareTo(String stringValue)

int compareTo(String anotherString)
Compares two strings lexicographically.
int compareToIgnoreCase(String str)
Compares two strings lexicographically, ignoring case differences.

The result of the compareTo(String anotherString) and compareToIgnoreCase(String str) is shown here:

ValueMeaning
Less than zeroThe invoking string is less than str.
Greater than zeroThe invoking string is greater than str.
ZeroThe two strings are equal.

public class Main {
  public static void main(String[] argv) {
    String str = "Java2s.com";
    String str2 = "java2s.com";
    System.out.println(str.compareTo(str2));
    System.out.println(str.compareToIgnoreCase(str2));

  }
}

The output:


-32
0

compareToIgnoreCase(String str) returns the same results as compareTo( ). It only ignores the case differences.


public class Main {

  public static void main(String args[]) {
    System.out.println("A".compareToIgnoreCase("a"));
  }
}
  

The output:


0
Home 
  Java Book 
    Essential Classes  

String:
  1. String type and Literals
  2. String Concatenation
  3. String.CASE_INSENSITIVE_ORDER
  4. String Constructor
  5. charAt(int index):Get a single char by index
  6. String: compareTo(String stringValue)
  7. concat(String str)
  8. equals():Compare two string value for equality
  9. equals( ) vs ==
  10. contains(CharSequence s)
  11. copyValueOf(char[] data)
  12. endsWith(String suffix)
  13. format():Format a string
  14. getBytes():Get byte array from string
  15. getChars()
  16. indexOf
  17. intern a string
  18. isEmpty:if string is empty
  19. lastIndexOf()
  20. length() Returns the length of this string
  21. startsWith( )
  22. toLowerCase() and toUpperCase(): convert string case with locale
  23. substring:Get sub string from a string
  24. toCharArray():Get char array from string
  25. toString( )
  26. trim()
  27. valueOf():Convert boolean, char, double, float,int,long,object to String