java compare strings lexicographically

‮l.www‬autturi.com
java compare strings lexicographically
  • 0 if the string is equal to the other string.
  • < 0 if the string is lexicographically less than the other string
  • 0 if the string is lexicographically greater than the other string (or has more characters)

/**
 * @author lautturi.com 
 * Java example:  compare two strings in java
 */


import java.util.*;

public class Lautturi {

	public static void main(String[] args) {
		
		String myStr1 = "Hello";
		String myStr2 = "Hello";
		System.out.println(myStr1.compareTo(myStr2));

	}
}

output:

0
Created Time:2017-09-25 23:52:35  Author:lautturi