Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (10.2k points)

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?

1 Answer

0 votes
by (46k points)

Yes, includes is case sensitive. You can apply java.util.regex.Pattern amidst the CASE_INSENSITIVE flag, for instance, insensitive matching:

Pattern.compile(Pattern.quote(wantedStr), Pattern.CASE_INSENSITIVE).matcher(source).find();

If s2 includes regex defined characters (of which there are many) it's necessary to extract it first. 

Related questions

0 votes
1 answer
0 votes
2 answers
asked Jul 9, 2019 in Java by Anvi (10.2k points)

Browse Categories