Java String compareTo()


The java string compareTo() method compares the given string with current string lexicographically. It returns positive number, negative number or 0.

It compares strings on the basis of Unicode value of each character in the strings.

If first string is lexicographically greater than second string, it returns positive number (difference of character value). If first string is less than second string lexicographically, it returns negative number and if first string is lexicographically equal to second string, it returns 0.

if s1 > s2, it returns positive number
if s1 < s2, it returns negative number
if s1 == s2, it returns 0



Signature

The signature or syntax of string trim method is given below:

public int compareTo(String anotherString)



Paremeters

anotherStringrepresents string that is to be compared with current string


Returns

an integer value


Java String compareTo() method example

Flash soon



Java String compareTo(): empty string

If you compare string with blank or empty string, it returns length of the string. If second string is empty, result would be positive. If first string is empty, result would be negative.

Flash soon



Copyright 2017 Design& Development