string.compareTo()

String.compareTo() Returns the difference. If both string are equal then 0 is returned.
Syntax
String1.compareTo(String2)
The output is an integer and the value is here

< 0 : If String1 is less than String2
= 0 : If String1 is equal to String2
< 1 : If String1 is greater than String2

Here are some examples with output.
String my_str1="Welcome to plus2net";
System.out.println(my_str1.compareTo("welcome to plus2net")); // -32
Note : Unicode value of W is 87 and lower case w is 119 ( difference is -32 )

In above code we change the value of my_str1 and make the strings equal.
String my_str1="welcome to plus2net";
System.out.println(my_str1.compareTo("welcome to plus2net")); // 0
Similarly we can change the other string
String my_str1="welcome to plus2net";
System.out.println(my_str1.compareTo("Welcome to plus2net")); // 32 
The string comparision is done lexicographically ( dictionary order and alphabetical order ).

Length of the string by using compareTo()

If our second string is blank then the output will be the lenght of the first string. Check this code.
String my_str1="plus2net";
System.out.println(my_str1.compareTo("")); // 8		


All String functions
Java

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer