The String.regionMatches()
method in Java is used to compare specific regions of two strings for equality.
Table of Contents
- Introduction
regionMatches
Method Syntax- Examples
- Basic Usage
- Using
regionMatches
with Case Insensitivity - Handling Edge Cases
- Real-World Use Case
- Conclusion
Introduction
The String.regionMatches()
method is a member of the String
class in Java. It allows you to compare specific regions of two strings to determine if they are equal. This method is useful when you need to compare substrings within larger strings.
regionMatches() Method Syntax
There are two overloaded versions of the regionMatches
method:
Case-Sensitive Comparison
public boolean regionMatches(int toffset, String other, int ooffset, int len)
- toffset: The starting index of the region in the current string.
- other: The string to compare.
- ooffset: The starting index of the region in the other string.
- len: The number of characters to compare.
Case-Insensitive Comparison
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
- ignoreCase: If true, the comparison ignores case; otherwise, the comparison is case-sensitive.
- toffset: The starting index of the region in the current string.
- other: The string to compare.
- ooffset: The starting index of the region in the other string.
- len: The number of characters to compare.
Examples
Basic Usage
The regionMatches
method can be used to compare specific regions of two strings.
Example
public class RegionMatchesExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "World";
boolean result = str1.regionMatches(7, str2, 0, 5);
System.out.println("Regions match: " + result);
}
}
Output:
Regions match: true
Using regionMatches
with Case Insensitivity
The regionMatches
method can also be used to compare regions of two strings, ignoring case differences.
Example
public class RegionMatchesIgnoreCaseExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "world";
boolean result = str1.regionMatches(true, 7, str2, 0, 5);
System.out.println("Regions match (case-insensitive): " + result);
}
}
Output:
Regions match (case-insensitive): true
Handling Edge Cases
Example: Regions Out of Bound
If the specified regions are out of bounds, the regionMatches
method returns false without throwing an exception.
public class RegionMatchesOutOfBoundsExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "Hello";
boolean result = str1.regionMatches(12, str2, 0, 5);
System.out.println("Regions match (out of bounds): " + result);
}
}
Output:
Regions match (out of bounds): false
Example: Comparing Different Lengths
If the length of the region to be compared exceeds the length of either string, the regionMatches
method returns false.
public class RegionMatchesDifferentLengthsExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "Hello, Java!";
boolean result = str1.regionMatches(0, str2, 0, 15);
System.out.println("Regions match (different lengths): " + result);
}
}
Output:
Regions match (different lengths): false
Real-World Use Case
Example: Validating Substrings within Larger Strings
One common use case for regionMatches
is validating that a specific part of a string matches a given pattern or substring.
public class ValidateSubstringExample {
public static void main(String[] args) {
String url = "https://www.example.com";
String protocol = "https";
if (url.regionMatches(true, 0, protocol, 0, protocol.length())) {
System.out.println("The URL uses HTTPS.");
} else {
System.out.println("The URL does not use HTTPS.");
}
}
}
Output:
The URL uses HTTPS.
In this example, the regionMatches
method is used to check if the URL starts with the "https" protocol, ignoring case differences.
Conclusion
The String.regionMatches()
method in Java is used for comparing specific regions of two strings. It supports both case-sensitive and case-insensitive comparisons, making it versatile for various applications. This method is particularly useful for validating and comparing substrings within larger strings. By understanding and utilizing the regionMatches
method, you can efficiently manage string region comparisons in your Java programs.