English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
We can use the following methods in Java to compare two strings in dictionary order.
Use the String.compareTo(String) method. It compares in a case-sensitive manner.
Use the String.compareToIgnoreCase(String) method. It compares in a case-insensitive manner.
Use the String.compareTo(Object) method. It compares in a case-sensitive manner.
These methods return the ASCII difference of the first odd character of the compared strings.
public class Tester { public static void main(String args[]) { String str = "Hello World"; String anotherString = "hello world"; Object objStr = str; System.out.println(str.compareTo(anotherString)); System.out.println(str.compareToIgnoreCase(anotherString)); System.out.println(str.compareTo(objStr.toString())); {} {}
Output Result
-32 0 0