Ticker

6/recent/ticker-posts

Compare Two Strings in Java — 11 Methods

Compare Two Strings in Java — 11 Methods

Introduction:
Comparing strings is a common operation in Java, and it allows you to determine if two strings have the same content or not. Java provides several methods to compare strings, each with its specific use case. In this documentation, we will explore 11 different methods to compare two strings in Java, along with code examples and explanations for each method.

Method 1: 

Using equals() method
The equals() method compares the content of two strings and returns a boolean value indicating whether the two strings are equal or not.

java
String str1 = "Hello";
String str2 = "Hello";
boolean result = str1.equals(str2);
System.out.println(result); // Output: true

Method 2: 

Using equalsIgnoreCase() method
The equalsIgnoreCase() method compares two strings while ignoring their case.

java
String str1 = "hello";
String str2 = "HELLO";
boolean result = str1.equalsIgnoreCase(str2);
System.out.println(result); // Output: true

Method 3: 

Using compareTo() method
The compareTo() method compares two strings lexicographically and returns an integer. It indicates whether the first string is lexicographically greater than, equal to, or less than the second string.

java
String str1 = "apple";
String str2 = "banana";
int result = str1.compareTo(str2);
System.out.println(result); // Output: Negative value

Method 4: Using compareToIgnoreCase() method
The compareToIgnoreCase() method compares two strings lexicographically while ignoring their case.

java
String str1 = "Apple";
String str2 = "banana";
int result = str1.compareToIgnoreCase(str2);
System.out.println(result); // Output: Negative value

Method 5: Using regionMatches() method
The regionMatches() method compares a specific region of two strings.

java
String str1 = "Hello, world!";
String str2 = "WORLD";
boolean result = str1.regionMatches(true, 7, str2, 0, 5);
System.out.println(result); // Output: true

Method 6: Using startsWith() method
The startsWith() method checks if a string starts with the specified prefix.

java
String str1 = "Hello, world!";
String prefix = "Hello";
boolean result = str1.startsWith(prefix);
System.out.println(result); // Output: true

Method 7: Using endsWith() method
The endsWith() method checks if a string ends with the specified suffix.

java
String str1 = "Hello, world!";
String suffix = "world!";
boolean result = str1.endsWith(suffix);
System.out.println(result); // Output: true

Method 8: Using contentEquals() method
The contentEquals() method compares the content of a StringBuffer or StringBuilder with a string.

java
String str1 = "Hello";
StringBuffer buffer = new StringBuffer("Hello");
boolean result = str1.contentEquals(buffer);
System.out.println(result); // Output: true

Method 9: Using equals() with null check
To avoid NullPointerException, you can use the equals() method with a null check.

java
String str1 = "Hello";
String str2 = null;
boolean result = str1.equals(str2);
System.out.println(result); // Output: false

Method 10: Using Objects.equals()
The Objects.equals() method compares two strings, handling the case of null values.

java
String str1 = "Hello";
String str2 = null;
boolean result = Objects.equals(str1, str2);
System.out.println(result); // Output: false

Method 11: Using StringUtils.equals() from Apache Commons Lang (Third-party library)
If you have Apache Commons Lang library, you can use StringUtils.equals() for comparison.

java
import org.apache.commons.lang3.StringUtils;

String str1 = "Hello";
String str2 = "hello";
boolean result = StringUtils.equals(str1, str2);
System.out.println(result); // Output: false

Conclusion:
In this documentation, we have explored 11 different methods to compare two strings in Java. Each method has its own use case, and you can choose the appropriate one based on your specific requirements. Remember to consider the case-sensitivity and null-checking while performing string comparisons in Java.

Post a Comment

0 Comments