String Regionmatch
## Java String regionMatches() Method: Comparing Substrings
In Java, comparing entire strings is a common task, but sometimes you only need to compare specific subregions (substrings) of two different strings. Instead of manually extracting substrings using `substring()` and then comparing them, Java provides a highly efficient, built-in method: `String.regionMatches()`.
This tutorial covers how the `regionMatches()` method works, its syntax, and practical code examples.
---
## Introduction to `regionMatches()`
The `regionMatches()` method tests whether a specified region of one string matches a specified region of another string.
This method is highly optimized because it performs the comparison directly on the character arrays of the strings without allocating new memory for temporary substring objects.
---
## Method Syntax
Java provides two overloaded versions of the `regionMatches()` method:
### 1. Case-Sensitive Comparison
```java
public boolean regionMatches(int toffset, String other, int ooffset, int len)
```
### 2. Case-Insensitive Comparison (with `ignoreCase` flag)
```java
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
```
### Parameter Definitions:
* **`ignoreCase`**: If `true`, character case is ignored during comparison.
* **`toffset`**: The starting offset (index) of the subregion in the calling string (the string invoking the method).
* **`other`**: The other string argument to compare against.
* **`ooffset`**: The starting offset (index) of the subregion in the `other` string.
* **`len`**: The number of characters to compare.
### Return Value:
* Returns `true` if the specified subregion of the calling string matches the specified subregion of the argument string; `false` otherwise.
---
## Code Example
The following example demonstrates how to use both versions of the `regionMatches()` method to perform case-sensitive and case-insensitive substring comparisons.
### `StringRegionMatch.java`
```java
public class StringRegionMatch {
public static void main(String[] args) {
String first_str = "Welcome to Microsoft";
String second_str = "I work with microsoft";
// 1. Case-sensitive comparison
// Compares 9 characters starting from index 11 of first_str ("Microsoft")
// with 9 characters starting from index 12 of second_str ("microsoft")
boolean match1 = first_str.regionMatches(11, second_str, 12, 9);
// 2. Case-insensitive comparison
// The first argument 'true' enables case-insensitivity
boolean match2 = first_str.regionMatches(true, 11, second_str, 12, 9);
// Output the results
System.out.println("Case-sensitive match result: " + match1);
System.out.println("Case-insensitive match result: " + match2);
}
}
```
### Output
```text
Case-sensitive match result: false
Case-insensitive match result: true
```
---
## Detailed Explanation of the Code
1. **`first_str.regionMatches(11, second_str, 12, 9)`**:
* It targets `first_str` starting at index `11` (which is the character `"M"` in `"Microsoft"`).
* It targets `second_str` starting at index `12` (which is the character `"m"` in `"microsoft"`).
* It compares `9` characters.
* Since `"Microsoft"` and `"microsoft"` differ in case (capital `"M"` vs lowercase `"m"`), and this overload is strictly case-sensitive, the method returns `false`.
2. **`first_str.regionMatches(true, 11, second_str, 12, 9)`**:
* By passing `true` as the first parameter, we instruct Java to ignore case differences.
* The comparison of `"Microsoft"` and `"microsoft"` now succeeds, returning `true`.
---
## Key Considerations
* **IndexOutOfBoundsException**: The method does not throw an exception if the indices are out of bounds. Instead, it safely returns `false` if:
* `toffset` or `ooffset` is negative.
* `toffset + len` is greater than the length of the calling string.
* `ooffset + len` is greater than the length of the `other` string.
* **Performance**: Using `regionMatches()` is more memory-efficient than `first_str.substring(11, 20).equals(second_str.substring(12, 21))` because it avoids creating new `String` objects in the heap.
YouTip