Compare To( ) Java Help

Compare To( )

Often, it is not enough to simply know whether two strings arc Identical. Sot sorting applications, you need to know which is less than, equal.to, or greater than the next. A string is less than another if it comes before the other in dictionary- order. A string is greater than other if it comes after the other in dictionary order. The String method compare To( ) serves this purpose. It has this general form: int compare To (String sir) Here, is the String being compared with the invoking String. The result of the comparison is returned and is interpreted as shown here: .

Here is a sample program that sorts an array of strings, The program uses . complete( ) to determine sort ordering .for a bubble sort: As you can see from the output of this example, compare To() takes into account uppercase and lowercase letters ..The word Ii Now” crane out before all the others because it begins with an uppercase letter, which means it has a lower value in the ASCII character set.

If you want to ignore case-differences when comparing two strings, use compare To Ignore Case ), shown here: int compare to lgnor everlasting st,) This method returns the same results as compare Sot ), except that case differences are ignored. This method was added by Java 2. You might want to try substituting it into the previous program. Aft?r doing so, “Now” will no longer be first.

JavaHelpOnline.com

  • Feel free to send us an inquiry, we will reply back in hours.
 

Verification

Posted on September 17, 2014 in Staring Handling

Share the Story

About the Author

Back to Top