Java String regionMatches() Method

📘 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

The String.regionMatches() method in Java is used to compare a specific region of one string with a region of another string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. regionMatches Method Syntax
  3. Examples
    • Basic Region Comparison
    • Case-Insensitive Region Comparison
    • Handling Edge Cases
  4. Conclusion

Introduction

The String.regionMatches() method is a member of the String class in Java. It allows you to compare a substring of one string to a substring of another string. This method is particularly useful when you need to compare parts of strings without creating new substrings.

regionMatches Method Syntax

The regionMatches method has two common variations:

  1. Case-sensitive comparison:
public boolean regionMatches(int toffset, String other, int ooffset, int len) 
  1. Case-insensitive comparison:
public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 
  • ignoreCase: If true, ignore case differences when comparing characters.
  • toffset: The starting offset in the first string.
  • other: The second string to be compared.
  • ooffset: The starting offset in the second string.
  • len: The number of characters to compare.

Examples

Basic Region Comparison

The regionMatches method can be used to compare regions of two strings in a case-sensitive manner.

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("Region matches: " + result); } } 

Output:

Region matches: true 

Case-Insensitive Region Comparison

The regionMatches method can also be used to compare regions of two strings in a case-insensitive manner.

Example

public class RegionMatchesExample { 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("Region matches (ignore case): " + result); } } 

Output:

Region matches (ignore case): true 

Handling Edge Cases

The regionMatches method returns false if the specified regions do not match.

Example

public class RegionMatchesExample { public static void main(String[] args) { String str1 = "Hello, World!"; String str2 = "Java"; boolean result = str1.regionMatches(7, str2, 0, 4); System.out.println("Region matches: " + result); } } 

Output:

Region matches: false 

Additionally, if the length of the region to be compared exceeds the length of either string, the method will also return false.

Example

public class RegionMatchesExample { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello, World!"; boolean result = str1.regionMatches(0, str2, 0, 10); System.out.println("Region matches: " + result); } } 

Output:

Region matches: false 

Conclusion

The String.regionMatches() method in Java is used for comparing specific regions of two strings. By understanding how to use this method, you can efficiently compare parts of strings in your Java applications. Whether you are performing case-sensitive or case-insensitive comparisons, the regionMatches method provides a reliable solution for these tasks.

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