TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java String compareTo, compareToIgnoreCase

Use compareTo to see if one string would be sorted in an earlier position than another.
CompareTo. In sorting, one string will come before or after another. Sometimes two strings are equal. With compareTo we determine lexical ordering.Strings
In Java, we call compareTo on a String instance. We pass one argument—this is the string we are comparing against the instance string.
First example. Let us test the compareTo method. The string "bird" should come before the string "cat." It is alphabetically earlier.

Result: We find that the compareTo method returns -1 when the earlier string is the instance, and the later string is the argument.

Java program that uses compareTo on Strings public class Program { public static void main(String[] args) { String value1 = "cat"; String value2 = "bird"; // Compare these strings. int result1 = value1.compareTo(value2); System.out.println("cat compareTo bird = " + result1); int result2 = value2.compareTo(value1); System.out.println("bird compareTo cat = " + result2); // Compare string against itself. // ... It is equal so the result is 0. int result3 = value1.compareTo(value1); System.out.println("cat compareTo cat = " + result3); } } Output cat compareTo bird = 1 bird compareTo cat = -1 cat compareTo cat = 0
CompareToIgnoreCase. With compareTo, strings are compared based on their ordinal values. In ASCII, uppercase letters have lower values than lowercase ones.ASCII Table

So: A lowercase letter will be sorted after an uppercase letter. Often this does not make sense for real programs.

Here: We use the compareToIgnoreCase method to treat lowercase and uppercase letters the same.

Java program that uses compareToIgnoreCase public class Program { public static void main(String[] args) { String value1 = "DOG"; String value2 = "bird"; // This is negative, so DOG comes before bird. int result1 = value1.compareTo(value2); System.out.println(result1); // Ignore case. // ... Now bird comes before dog (the result is positive). int result2 = value1.compareToIgnoreCase(value2); System.out.println(result2); } } Output -30 2
Result value. The key part about compareTo (and compareToIgnoreCase) is that the result int is negative, positive, or zero. These mean different sort orders.

Negative: If compareTo returns a negative number, the instance string comes before the argument.

Positive: When a positive number is returned, the instance string comes after the argument string.

Zero: A zero is returned when the two strings have an equal sorting order. They are equal in content.

A final note, sorting. With compareTo and compareToIgnoreCase, we often implement a sorting method. We can directly return the result of compareTo.Sort
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf