Java String regionMatches() Method

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