Title: String CompareTo Java Example - Java Code Geeks
Open Graph Title: String CompareTo Java Example - Java Code Geeks
Description: In this post, we feature a comprehensive String CompareTo Java Example. In a previous post, Java Compare Strings Example, we showed how to compare two
Open Graph Description: In this post, we feature a comprehensive String CompareTo Java Example. In a previous post, Java Compare Strings Example, we showed how to compare two
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/string-compareto-java-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/string-compareto-java-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/string-compareto-java-example/"},"author":{"name":"Nikos Maravitsas","@id":"https://examples.javacodegeeks.com/#/schema/person/686a494fbb577a8d1231862b139cda6b"},"headline":"String CompareTo Java Example","datePublished":"2014-03-10T17:36:18+00:00","dateModified":"2021-05-13T15:13:33+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/string-compareto-java-example/"},"wordCount":489,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/string-compareto-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["String"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/string-compareto-java-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/string-compareto-java-example/","url":"https://examples.javacodegeeks.com/string-compareto-java-example/","name":"String CompareTo Java Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/string-compareto-java-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/string-compareto-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-03-10T17:36:18+00:00","dateModified":"2021-05-13T15:13:33+00:00","description":"In this post, we feature a comprehensive String CompareTo Java Example. In a previous post, Java Compare Strings Example, we showed how to compare two","breadcrumb":{"@id":"https://examples.javacodegeeks.com/string-compareto-java-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/string-compareto-java-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/string-compareto-java-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/string-compareto-java-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"lang","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/"},{"@type":"ListItem","position":5,"name":"String","item":"https://examples.javacodegeeks.com/category/java-development/core-java/lang/string/"},{"@type":"ListItem","position":6,"name":"String CompareTo Java Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/686a494fbb577a8d1231862b139cda6b","name":"Nikos Maravitsas","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Nikos-Maravitsas-80x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Nikos-Maravitsas-80x96.jpg","caption":"Nikos Maravitsas"},"description":"Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.","sameAs":["http://www.javacodegeeks.com/"],"url":"https://examples.javacodegeeks.com/author/nikos-maravitsas/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"lang","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/"}},{"@type":"ListItem","position":5,"item":{"name":"String","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/lang\/string\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-03-10T19:36:18+02:00","datePublished":"2014-03-10T19:36:18+02:00","dateModified":"2021-05-13T18:13:33+03:00","headline":"String CompareTo Java Example","name":"String CompareTo Java Example","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/string-compareto-java-example\/","description":"In this post, we feature a comprehensive String CompareTo Java Example. In a previous post, Java Compare Strings Example, we showed how to compare two Strings in java. In that example the test was sim","copyrightYear":"2014","articleSection":"String","articleBody":"\nIn this post, we feature a comprehensive String CompareTo Java Example. In a previous post, Java Compare Strings Example, we showed how to compare two Strings in java. In that example the test was simple: check if two String objects are equal. But consider the case when you have a collection of Strings and you want to sort it. Of course, equality check is not enough. You have to impose ordering somehow. In strings (words in general), you can use lexicographical ordering.\n\n\n\n\n\n\n\nLexicographical ordering, or dictionary ordering or alphabetic ordering, is the operation of sorting strings alphabetically as they would appear on a dictionary. The process of doing so is very simple.\n\n\n\nImagining two Strings as an array of characters, then two strings are different if one of the following holds:\n\n\n\nThey have a different character in the same positionThey differ in lengthBoth of the above\n\n\n\nThe sorting is done like so:\n\n\n\nSequentially scanning the strings, find the first (common) position (or index) in which they have a different character.Compare the two characters using '<' or '>'.The string with the smaller value lexicographically precedes the other.If there is no index position at which they have a different characher, then the shorter string lexicographically precedes the longer string.\n\n\n\nIt is that simple. And it is equally simple to implement that algorithm in Java. But you don't have to bother as the String class API offers methods that do just that. These methods are compareTo and compareToIngonreCase.\n\n\n\n1. String CompareTo Java Example\n\n\n\ncompareTo will lexicographically compare two strings ad return a negative number if the first string is \"smaller\" than the second, zero if the strings are equal or a positive number if the first string is \"bigger\" than the second. Now that returning number is calculated like so:\n\n\n\nIf a different character is found in the same position, let it be i, of the two strings the compareTo will return s1.charaAt(i) - s2.charAt(i).If there is no index position at which they have a different character, compareTo will return s1.lenght() - s2.length();\n\n\n\nLet's see how you can use it:\n\n\n\nStringCompareToExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n\n\n\npackage com.javacodegeeks.core.lang.string;\n \npublic class StringCompareToExample {\n \n public static void main(String[] args) {\n \n String a = \"abcd\";\n String b = \"abce\";\n String c = \"abcd\";\n \n System.out.println(a.compareTo(b));\n System.out.println(c.compareTo(a));\n \n b = \"abcde\";\n \n System.out.println(a.compareTo(b));\n \n a = \"a random string\";\n b = \"another string \";\n \n System.out.println(b.compareTo(a));\n \n }\n}\n\n\n\n\n\n\n\n\n\n\nThe above prints out:\n\n\n\n-1\n0\n-1\n78\n\n\n\n\nIt is important to note that compareTo compares the strings based on the Unicode value of each character in the strings.\n\n\n\n2. Using compareToIgnoreCase\n\n\n\nYou can use compareToIgnoreCase to lexicographically sort strings without taking case into consideration, e.g for a case-insensitive ordering. Internally it uses : Character.toLowerCase(Character.toUpperCase(character)) to convert all the characters of both strings to lower case.\n\n\n\nLet's see how you can use it:\n\n\n\nStringCompareToExample.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n\n\n\npackage com.javacodegeeks.core.lang.string;\n \npublic class StringCompareToExample {\n \n public static void main(String[] args) {\n \n String st = \"abcd\";\n String st2 = \"abce\";\n String st3 = \"aBcE\";\n \n System.out.println(st.compareToIgnoreCase(st2));\n System.out.println(st.compareToIgnoreCase(st3));\n System.out.println(st2.compareToIgnoreCase(st3));\n \n }\n}\n\n\n\n\n\n\n\n\n\n\nThe above prints out:\n\n\n\n-1\n-1\n0\n\n\n\nYou can also check the Comparable Java Example to learn more!\n\n\n\n3. More articles\n\n\n\nJava String Class ExampleString to Int Java ExampleConvert int to string Java Example\n\n\n\n4. Download the source code\n\n\n\nThis was a Java String CompareTo example.\nDownload\nYou can download the code of this tutorial here : String CompareTo Java Example\n\n\n\nLast updated on May 13th, 2021\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/string-compareto-java-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Nikos Maravitsas","url":"https:\/\/examples.javacodegeeks.com\/author\/nikos-maravitsas\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-03-10T17:36:18+00:00 |
| article:modified_time | 2021-05-13T15:13:33+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Nikos Maravitsas |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Nikos Maravitsas |
| twitter:label2 | Est. reading time |
| twitter:data2 | 4 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1