String Comparison Methods in Java

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

Introduction

Comparing strings is a common task in Java programming. Java provides several methods to compare strings based on their content, lexicographical order, and more. This tutorial will cover the various methods available in Java for string comparison with examples.

Table of Contents

  1. equals()
  2. equalsIgnoreCase()
  3. compareTo()
  4. compareToIgnoreCase()
  5. contentEquals()
  6. regionMatches()
  7. startsWith() and endsWith()
  8. Complete Example Program
  9. Conclusion

1. equals()

The equals() method compares two strings for equality. It returns true if the strings are equal and false otherwise. This comparison is case-sensitive.

Example:

public class StringEqualsExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; String str3 = "hello"; System.out.println("str1 equals str2: " + str1.equals(str2)); // true System.out.println("str1 equals str3: " + str1.equals(str3)); // false } } 

Output:

str1 equals str2: true str1 equals str3: false 

2. equalsIgnoreCase()

The equalsIgnoreCase() method compares two strings for equality, ignoring case differences.

Example:

public class StringEqualsIgnoreCaseExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = "hello"; System.out.println("str1 equalsIgnoreCase str2: " + str1.equalsIgnoreCase(str2)); // true } } 

Output:

str1 equalsIgnoreCase str2: true 

3. compareTo()

The compareTo() method compares two strings lexicographically. It returns:

  • A negative integer if the first string is lexicographically less than the second string.
  • Zero if the first string is lexicographically equal to the second string.
  • A positive integer if the first string is lexicographically greater than the second string.

Example:

public class StringCompareToExample { public static void main(String[] args) { String str1 = "Apple"; String str2 = "Banana"; String str3 = "Apple"; System.out.println("str1 compareTo str2: " + str1.compareTo(str2)); // negative System.out.println("str1 compareTo str3: " + str1.compareTo(str3)); // zero System.out.println("str2 compareTo str1: " + str2.compareTo(str1)); // positive } } 

Output:

str1 compareTo str2: -1 str1 compareTo str3: 0 str2 compareTo str1: 1 

4. compareToIgnoreCase()

The compareToIgnoreCase() method compares two strings lexicographically, ignoring case differences.

Example:

public class StringCompareToIgnoreCaseExample { public static void main(String[] args) { String str1 = "Apple"; String str2 = "apple"; System.out.println("str1 compareToIgnoreCase str2: " + str1.compareToIgnoreCase(str2)); // zero } } 

Output:

str1 compareToIgnoreCase str2: 0 

5. contentEquals()

The contentEquals() method compares the content of a string with a StringBuffer or CharSequence. It returns true if the content matches and false otherwise.

Example:

public class StringContentEqualsExample { public static void main(String[] args) { String str1 = "Hello"; StringBuffer str2 = new StringBuffer("Hello"); System.out.println("str1 contentEquals str2: " + str1.contentEquals(str2)); // true } } 

Output:

str1 contentEquals str2: true 

6. regionMatches()

The regionMatches() method compares a specific region of one string with a specific region of another string.

Example:

public class StringRegionMatchesExample { public static void main(String[] args) { String str1 = "HelloWorld"; String str2 = "World"; System.out.println("str1 regionMatches: " + str1.regionMatches(5, str2, 0, 5)); // true } } 

Output:

str1 regionMatches: true 

7. startsWith() and endsWith()

The startsWith() method checks if a string starts with a specified prefix, and the endsWith() method checks if a string ends with a specified suffix.

Example:

public class StringStartsWithEndsWithExample { public static void main(String[] args) { String str = "HelloWorld"; System.out.println("str startsWith 'Hello': " + str.startsWith("Hello")); // true System.out.println("str endsWith 'World': " + str.endsWith("World")); // true } } 

Output:

str startsWith 'Hello': true str endsWith 'World': true 

8. Complete Example Program

Here is a complete program that demonstrates the various string comparison methods discussed above.

Example Code:

public class StringComparisonExamples { public static void main(String[] args) { String str1 = "Hello"; String str2 = "hello"; String str3 = "Apple"; String str4 = "Banana"; StringBuffer strBuffer = new StringBuffer("Hello"); String str5 = "HelloWorld"; String str6 = "World"; // equals() method System.out.println("str1 equals str2: " + str1.equals(str2)); // false // equalsIgnoreCase() method System.out.println("str1 equalsIgnoreCase str2: " + str1.equalsIgnoreCase(str2)); // true // compareTo() method System.out.println("str3 compareTo str4: " + str3.compareTo(str4)); // negative // compareToIgnoreCase() method System.out.println("str1 compareToIgnoreCase str2: " + str1.compareToIgnoreCase(str2)); // zero // contentEquals() method System.out.println("str1 contentEquals strBuffer: " + str1.contentEquals(strBuffer)); // true // regionMatches() method System.out.println("str5 regionMatches: " + str5.regionMatches(5, str6, 0, 5)); // true // startsWith() and endsWith() methods System.out.println("str5 startsWith 'Hello': " + str5.startsWith("Hello")); // true System.out.println("str5 endsWith 'World': " + str5.endsWith("World")); // true } } 

Output:

str1 equals str2: false str1 equalsIgnoreCase str2: true str3 compareTo str4: -1 str1 compareToIgnoreCase str2: 0 str1 contentEquals strBuffer: true str5 regionMatches: true str5 startsWith 'Hello': true str5 endsWith 'World': true 

9. Conclusion

Java provides various methods for comparing strings, each suited for different needs. The equals() and equalsIgnoreCase() methods check for content equality, while compareTo() and compareToIgnoreCase() methods provide lexicographical comparison. The contentEquals(), regionMatches(), startsWith(), and endsWith() methods offer additional flexibility for specific comparison scenarios. Understanding these methods and their use cases is essential for effective string manipulation and comparison in Java.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare