Say I have two strings,
String s1 = "AbBaCca";
String s2 = "bac";
I want to perform a check returning that s2
is contained within s1
. I can do this with:
return s1.contains(s2);
I am pretty sure that contains()
is case sensitive, however I can't determine this for sure from reading the documentation. If it is then I suppose my best method would be something like:
return s1.toLowerCase().contains(s2.toLowerCase());
All this aside, is there another (possibly better) way to accomplish this without caring about case-sensitivity?
Yes, this is achievable:
This code will return the String "TRUE!" as it found that your characters were contained.
A Faster Implementation: Utilizing
String.regionMatches()
Using regexp can be relatively slow. It (being slow) doesn't matter if you just want to check in one case. But if you have an array or a collection of thousands or hundreds of thousands of strings, things can get pretty slow.
The presented solution below doesn't use regular expressions nor
toLowerCase()
(which is also slow because it creates another strings and just throws them away after the check).The solution builds on the String.regionMatches() method which seems to be unknown. It checks if 2
String
regions match, but what's important is that it also has an overload with a handyignoreCase
parameter.Speed Analysis
This speed analysis does not mean to be rocket science, just a rough picture of how fast the different methods are.
I compare 5 methods.
String.contains()
.String.contains()
with the pre-cached, lower-cased substring. This solution is already not as flexible because it tests a predefiend substring.Pattern.compile().matcher().find()
...)Pattern
. This solution is already not as flexible because it tests a predefined substring.Results (by calling the method 10 million times):
Pattern
: 1845 msResults in a table:
Our method is 4x faster compared to lowercasing and using
contains()
, 10x faster compared to using regular expressions and also 3x faster even if thePattern
is pre-cached (and losing flexibility of checking for an arbitrary substring).Analysis Test Code
If you're interested how the analysis was performed, here is the complete runnable application:
A simpler way of doing this (without worrying about pattern matching) would be converting both
String
s to lowercase:If you have to search an ASCII string in another ASCII string, such as a URL, you will find my solution to be better. I've tested icza's method and mine for the speed and here are the results:
The code: