Java String regionMatches() Method with Examples
In Java, the regionMatches() method is part of the String class, providing a mechanism to compare specific regions of two strings. It enables you to check if a portion of one string matches a portion of another string, either with or without considering the case of the characters.
Method 1: boolean regionMatches(int toffset, String other, int ooffset, int len)
The boolean regionMatches(int toffset, String other, int ooffset, int len) method in Java is part of the String class and is used to compare a specific region of the invoking (source) string with a specific region of another (target) string. The comparison is case-sensitive, meaning it distinguishes between uppercase and lowercase characters.
Parameters
- toffset: This is the starting index of the substring in the invoking string (this object).
- other: The string against which the comparison is made.
- ooffset: The starting index of the substring in the specified string other.
- len: The number of characters to compare.
Examples
1. Basic string comparison
Basic string comparison using the boolean regionMatches(int toffset, String other, int ooffset, int len) method involves comparing a specific region of one string with a specific region of another string. This method is case-sensitive, meaning it distinguishes between uppercase and lowercase characters.
Filename: BasicStringComparisonExample.java
public class BasicStringComparisonExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "Hello";
// Compare the first 5 characters of str1 with str2
boolean result = str1.regionMatches(0, str2, 0, 5);
if (result) {
System.out.println("The specified regions match.");
} else {
System.out.println("The specified regions do not match.");
}
}
}
Output:
The specified regions match.
In this example, str1.regionMatches(0, str2, 0, 5) compares the substring of str1 starting from index 0 (inclusive) and having a length of 5 characters with the substring of str2 starting from index 0 (inclusive). If the specified regions match, the program prints "The specified regions match," otherwise it prints "The specified regions do not match."
2. Checking a particular substring
Checking a particular substring using the boolean regionMatches(int toffset, String other, int ooffset, int len) method in Java involves comparing a specific portion of one string with a substring of another string. This method is useful when you want to determine if a particular substring exists at a specific position in a larger string. The comparison is case-sensitive, meaning it differentiates between uppercase and lowercase characters.
Filename: SubstringCheckExample.java
public class SubstringCheckExample {
public static void main(String[] args) {
String sentence = "The quick brown fox jumps over the lazy dog.";
// Check if the word "brown" appears at index 10
boolean result = sentence.regionMatches(10, "brown", 0, 5);
if (result) {
System.out.println("The specified regions match.");
} else {
System.out.println("The specified regions do not match.");
}
}
}
Output:
The specified regions match.
In this example, sentence.regionMatches(10, "brown", 0, 5) compares a substring of sentence starting from index 10 (inclusive) and having a length of 5 characters with the string "brown". If the specified regions match, the program prints "The specified regions match," otherwise it prints "The specified regions do not match."
3. Case-sensitive comparison
A case-sensitive comparison using the boolean regionMatches(int toffset, String other, int ooffset, int len) method in Java involves comparing a specific region of one string with a specific region of another string, taking into account the case of the characters. This means that uppercase and lowercase characters are treated as distinct during the comparison.
Filename: CaseSensitiveComparisonExample.java
public class CaseSensitiveComparisonExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "hello";
// Compare the first 5 characters of str1 with str2, case-sensitive
boolean result = str1.regionMatches(0, str2, 0, 5);
if (result) {
System.out.println("The specified regions match.");
} else {
System.out.println("The specified regions do not match.");
}
}
}
Output:
The specified regions do not match.
In this example, str1.regionMatches(0, str2, 0, 5) compares a substring of str1 starting from index 0 (inclusive) and having a length of 5 characters with the substring of str2 starting from index 0 (inclusive). Since the comparison is case-sensitive, the program prints "The specified regions do not match" since "Hello" and "hello" differ in case.
4. Password Validation
Password validation using the boolean regionMatches(int toffset, String other, int ooffset, int len) method in Java involves comparing a specific region of an entered password with an expected pattern or substring. This method can be used to check if a password adheres to a specific format or has certain characteristics. The comparison is case-sensitive, meaning it differentiates between uppercase and lowercase characters.
Filename: PasswordValidationExample.java
public class PasswordValidationExample {
public static void main(String[] args) {
String enteredPassword = "P@ssw0rd";
// Checking if the entered password matches the expected pattern, starting from index 0
boolean result = enteredPassword.regionMatches(0, "P@ss", 0, 4);
if (result) {
System.out.println("Password is valid.");
} else {
System.out.println("Invalid password.");
}
}
}
Output:
Password is valid.
In this example, enteredPassword.regionMatches(0, "P@ss", 0, 4) checks whether the substring of enteredPassword starting from index 0 (inclusive) and having a length of 4 characters matches the string "P@ss". If the specified regions match, the program prints "Password is valid," otherwise it prints "Invalid password."
Method 2: boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
The boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) method in Java is part of the String class and is used to compare a specific region of one string with a specific region of another string. This method provides an additional parameter, ignoreCase, which allows for a case-insensitive comparison.
Parameters
- ignoreCase: If true, the comparison is case-insensitive. If false, the comparison is case-sensitive.
- toffset: The starting index of the substring in the invoking string.
- other: The string against which the comparison is made.
- ooffset: The starting index of the substring in the specified string other.
- len: The number of characters to compare.
Examples
1. Basic Case-insensitive String Comparison
A basic case-insensitive string comparison using the boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) method in Java involves comparing a specific region of one string with a specific region of another string, ignoring the case of the characters. This method is useful when you want to check if a part of one string matches a specific part of another string, regardless of whether the characters are in uppercase or lowercase.
Filename: CaseInsensitiveComparisonExample.java
public class CaseInsensitiveComparisonExample {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = "hello";
// Compare the first 5 characters of str1 with str2, ignoring case
boolean result = str1.regionMatches(true, 0, str2, 0, 5);
if (result) {
System.out.println("The specified regions match (ignoring case).");
} else {
System.out.println("The specified regions do not match.");
}
}
}
Output:
The specified regions match (ignoring case).
In this example, str1.regionMatches(true, 0, str2, 0, 5) compares a substring of str1 starting from index 0 (inclusive) and having a length of 5 characters with the substring of str2 starting from index 0 (inclusive). Since the comparison is case-insensitive (due to ignoreCase being true), the program prints "The specified regions match (ignoring case)" even though "Hello" and "hello" differ in case.
2. Case-insensitive Substring Check
A case-insensitive substring check using the boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) method in Java involves verifying whether a specific substring in one string matches another substring in a different string, ignoring the case of the characters. This method is beneficial when you want to determine if a particular part of one string exists in another string, regardless of the letter casing.
Filename: CaseInsensitiveSubstringCheckExample.java
public class CaseInsensitiveSubstringCheckExample {
public static void main(String[] args) {
String sentence = "The quick brown fox jumps over the lazy dog.";
// Check if the word "Brown" appears at index 10, ignoring case
boolean result = sentence.regionMatches(true, 10, "Brown", 0, 5);
if (result) {
System.out.println("The specified regions match (ignoring case).");
} else {
System.out.println("The specified regions do not match.");
}
}
}
Output:
The specified regions match (ignoring case).
In this example, sentence.regionMatches(true, 10, "Brown", 0, 5) checks whether the substring of sentence starting from index 10 (inclusive) and having a length of 5 characters matches the string "Brown". Since the comparison is case-insensitive (due to ignoreCase being true), the program prints "The specified regions match (ignoring case)" even though "brown" and "Brown" differ in case.
3. Ignoring case in password validation
Ignoring case in password validation using the boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) method in Java involves checking if a specific region of an entered password matches an expected pattern or substring, while disregarding the case of the characters. This method is useful when you want to ensure that a password adheres to a specific format or has certain characteristics without being sensitive to the case of the characters.
Filename: CaseInsensitivePasswordValidationExample.java
public class CaseInsensitivePasswordValidationExample {
public static void main(String[] args) {
String enteredPassword = "P@ssw0rd";
// Checking if the entered password matches the expected pattern, ignoring case
boolean result = enteredPassword.regionMatches(true, 0, "p@ss", 0, 4);
if (result) {
System.out.println("Password is valid.");
} else {
System.out.println("Invalid password.");
}
}
}
Output:
Password is valid.
In this example, enteredPassword.regionMatches(true, 0, "p@ss", 0, 4) checks whether the substring of enteredPassword starting from index 0 (inclusive) and having a length of 4 characters matches the string "p@ss". Since the comparison is case-insensitive (due to ignoreCase being true), the program prints "Password is valid" even though the expected pattern "p@ss" and the entered password "P@ssw0rd" differ in case.