Home
Map
String regionMatches ExampleUse the regionMatches string method to test parts of strings. Avoid creating substrings.
Java
This page was last reviewed on May 29, 2023.
RegionMatches. This method tests a region of one string against another. We provide a start index for both strings. We also provide a length argument (the number of chars to test on both).
With regionMatches, we can see if a substring patches another substring. We can avoid creating substrings. This saves allocations and improves performance.
An example. Let us begin with this example. We have a string literal with four concatenated words in it. We match each part of this literal.
Detail We can have regionMatches ignore the case of characters by passing a value of "true" as the first argument.
public class Program { public static void main(String[] args) { String codes = "onetwothreeFour"; // Check for "two" at index 3. // ... Length is 3 chars. if (codes.regionMatches(3, "two", 0, 3)) { System.out.println(true); } // Check for "three" at index 6. // ... Length is 5 chars. if (codes.regionMatches(6, "three", 0, 5)) { System.out.println(true); } // Check for "FOUR" from "IS_FOUR" at index 11. // ... Length is 4 chars. if (codes.regionMatches(true, 11, "IS_FOUR", 3, 4)) { System.out.println(true); } } }
true true true
Benchmark. Here is a benchmark of regionMatches. We can use regionMatches for some significant performance wins in real-world Java programs.
Version 1 In this version of the code, we use the regionMatches method to compare 2 substring parts.
Version 2 In this code we make two substring calls. Those substrings are compared with equals().
String equals
Result RegionMatches is faster. In this benchmark regionMatches is over 10 times faster than the version that uses substring and equals.
public class Program { public static void main(String[] args) { String part1 = "catdogbird"; String part2 = "dogbirdcat"; // Test two versions. if (part1.regionMatches(3, part2, 0, 3)) { System.out.println("True 1"); } if (part1.substring(3, 6).equals(part2.substring(0, 3))) { System.out.println("True 2"); } int count = 0; long t1 = System.currentTimeMillis(); // Version 1: use regionMatches to compare two substrings. for (int i = 0; i < 10000000; i++) { if (part1.regionMatches(3, part2, 0, 3)) { count++; } } long t2 = System.currentTimeMillis(); // Version 2: use substring to compare two substrings. for (int i = 0; i < 10000000; i++) { if (part1.substring(3, 6).equals(part2.substring(0, 3))) { count++; } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(count); System.out.println(t2 - t1); System.out.println(t3 - t2); } }
True 1 True 2 20000000 15 ms: regionMatches 172 ms: substring, equals
Notes, indexes. When using methods like regionMatches and substring(), using correct indexes and lengths is important. The final argument on regionMatches is a length.
String substring
String length
Performance. Avoiding allocations is one of the best performance optimizations. With regionMatches, we can achieve a big performance win if we can reduce substring allocations.
A summary. The arguments to regionMatches can be hard to get right at first. But the performance and code size improvements with regionMatches makes it worth the effort.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.