Latest web development tutorials

Java Examples - string comparison

Java Examples Java Examples

The following examples we string function compareTo (string), compareToIgnoreCase (String) and compareTo (object string) to compare two strings and returns the ASCII string in the first letter of the difference.

Examples of code is as follows:

//StringCompareEmp.java 文件

public class StringCompareEmp{
   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()));
   }
}

The output is the above code examples:

-32
0
0

Java Examples Java Examples