Core JAVA

How to Compare two Strings in Java

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img title&equals;"Compare two Strings in Java" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;02&sol;Java-program-to-compare-two-strings&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p>In Java application string comparison is very common requirement in the all real world scenarios&period; So it is very basic need to know how to compare two string values each other&period; Although Java provide different ways to compare two string each other in an application&period; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;02&sol;why-string-is-immutable-and-final-in-java&period;html">String is immutable in nature<&sol;a> and so its immutability nature has lots of benefits like Security&comma; String Pool&comma; and Performance&period;<&sol;p>&NewLine;<p>In this tutorial of string comparison we will describe different ways to compare two String in Java&period; There are two methods equals&lpar;&rpar; or compareTo&lpar;&rpar; in Java compare two string values to each other&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>There are following examples of string comparison in java&period; Let&&num;8217&semi;s see below String comparison&colon;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>String comparison using equals method<&sol;li>&NewLine;<li>String comparison using equalsIgnoreCase method<&sol;li>&NewLine;<li>String comparison using compareTo method<&sol;li>&NewLine;<li>String comparison using compareToIgnoreCase method<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2><b>String comparison using <em>equals<&sol;em> method<&sol;b><&sol;h2>&NewLine;<p>As we all know that equals&lpar;&rpar; method of object class is using for compare two objects for semantic comparison&period; String class overrides equals&lpar;&rpar; method of object class for content equality&period; So if two strings contain same characters&comma; in same order and in same case they will be equals by equals&lpar;&rpar; method&period; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;02&sol;hashcode-and-equals-methods-in-java&period;html">Read more about equals&lpar;&rpar; method in java from this article&period;<&sol;a><&sol;p>&NewLine;<p><b>For example <&sol;b><br &sol;>&NewLine;We will compare two strings &&num;8220&semi;Java&&num;8221&semi; and &&num;8220&semi;Java&&num;8221&semi; by equals&lpar;&rpar; method&comma; it will return true but for &&num;8220&semi;Java&&num;8221&semi; and &&num;8220&semi;JAVA&&num;8221&semi; it will return false&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Java"&period;equals&lpar;"Java"&rpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Java"&period;equals&lpar;"JAVA"&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>String comparison using <em>equalsIgnoreCase<&sol;em> method<&sol;b><&sol;h2>&NewLine;<p>In java equalsIgnoreCase&lpar;&rpar; method is same as equals&lpar;&rpar; method but it is more liberal than equals and it compare two strings ignoring their case&period; That is if two strings have same characters and in same order despite of their case then equalsIgnoreCase&lpar;&rpar; method will always return true&period; equalsIgnoreCase&lpar;&rpar; method compare two strings by case insensitive i&period;e strings are either in lower case&comma; upper case&comma; Capital case or Camel case they will be equal by equalsIgnoreCase&period;<&sol;p>&NewLine;<p><b>For example <&sol;b><br &sol;>&NewLine;There were two strings of earlier example &&num;8220&semi;Java&&num;8221&semi; and &&num;8220&semi;JAVA&&num;8221&semi;&period; Now we compare these strings by equalsIgnoreCase&lpar;&rpar; method and it will return true but &&num;8220&semi;Java&&num;8221&semi; and &&num;8220&semi;Dinesh on Java&&num;8221&semi; will not be same and it will return false because they don&&num;8217&semi;t contain same characters&period;<&sol;p>&NewLine;<pre class&equals;"highlight">public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Java"&period;equalsIgnoreCase&lpar;"Java"&rpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Java"&period;equalsIgnoreCase&lpar;"JAVA"&rpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Java"&period;equalsIgnoreCase&lpar;"Dinesh on Java"&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>String comparison using <em>compareTo<&sol;em> method<&sol;b><&sol;h2>&NewLine;<p>In java String class overrides compareTo&lpar;&rpar; method of Compareable interface&period; compareTo&lpar;&rpar; method compares two strings in different manner unlike equals&lpar;&rpar; and equalsIgnoreCase&lpar;&rpar;&comma; it actually compares two string lexicographically&period; This means if two strings lexicographically equal&comma; precedes or follows each other&period; compareTo&lpar;&rpar; method is commonly used for sorting of strings lexicographically &period; This is also called natural order of String&period; compareTo&lpar;&rpar; method return int value if it returns zero if two strings are same&comma; if it returns less than zero if calling string comes before argument string and if it returns greater than zero if calling string comes later than argument string&period;<&sol;p>&NewLine;<p><b>For example&colon;<&sol;b><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>There are two string &&num;8220&semi;Java&&num;8221&semi; and &&num;8220&semi;Java&&num;8221&semi;&period; If we will compare these strings by compareTo&lpar;&rpar; method it will return 0&period;<&sol;li>&NewLine;<li>There are two string &&num;8220&semi;CPP&&num;8221&semi; and &&num;8220&semi;Java&&num;8221&semi;&period; If we will compare these strings by compareTo&lpar;&rpar; method it will return -7&period;<&sol;li>&NewLine;<li>There are two string &&num;8220&semi;Java&&num;8221&semi; and &&num;8220&semi;CPP&&num;8221&semi;&period; If we will compare these strings by compareTo&lpar;&rpar; method it will return 7&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre class&equals;"highlight">public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Java"&period;compareTo&lpar;"Java"&rpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"CPP"&period;compareTo&lpar;"Java"&rpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"Java"&period;compareTo&lpar;"CPP"&rpar;&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>String comparison using <em>compareToIgnoreCase<&sol;em> method<&sol;b><&sol;h2>&NewLine;<p>compareToIgnoreCase&lpar;&rpar; method is similar to compareTo&lpar;&rpar; method with ignoring case like equalsIgnoreCase&lpar;&rpar; and return same values as returned by compareTo during String comparison&period;<&sol;p>&NewLine;<h2><b>Summary<&sol;b><&sol;h2>&NewLine;<p>We have seen multiple ways to compare two strings each other&period; Now it depends on your requirement which is suitable for you&period; If you want to compare two strings with case sensitive then go with equals&lpar;&rpar; method if case is not matter in comparison of two strings then compare with equalsIgnoreCase&lpar;&rpar; method&period; If want to compare two strings lexicographically equal then use compareTo&lpar;&rpar; method&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;">&NewLine;<h2><b>String Related Posts<&sol;b><&sol;h2>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-to-make-java-class-immutable&sol;">How to make Immutable Class in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-interview-questions&sol;">Core Java Interview Questions<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;string-class-in-java&sol;">String Class in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;why-string-is-immutable-and-final-in-java&sol;">Why String is Immutable and Final in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;convert-string-to-integer-to-string-in-java-with-example&sol;">Convert String to Integer to String in Java with Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;convert-double-to-string-to-double-in-java-with-example&sol;">Convert Double to String to Double in Java with Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;convert-date-to-string-in-java-with-example&sol;">Convert Date to String in Java with Example<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;differences-between-string-stringbuffer-and-stringbuilder-in-java&sol;">Differences between String&comma; StringBuffer and StringBuilder in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-stringbuilder-and-stringbuffer-in-java&sol;">Differences StringBuffer and StringBuilder<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;how-to-compare-two-strings-in-java&sol;">How to Compare two Strings in Java<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hashcode-and-equals-methods-in-java&sol;">Difference between hashCode&lpar;&rpar; and equals&lpar;&rpar; methods in Java<br &sol;>&NewLine;<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;java-string-substring-method&sol;"> Java &&num;8211&semi; String substring&lpar;&rpar; Method<br &sol;>&NewLine;<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-wait-and-sleep&sol;"> Difference between wait&lpar;&rpar; and sleep&lpar;&rpar;<br &sol;>&NewLine;<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-stringbuilder-and-stringbuffer-in-java&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;convert-double-to-string-to-double-in-java-with-example&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '68'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago