How to check if a String contains another String in a case insensitive manner in Java? -
say have 2 strings,
string s1 = "abbacca"; string s2 = "bac"; i want perform check returning s2 contained within s1. can with:
return s1.contains(s2); i pretty sure contains() case sensitive, can't determine sure reading documentation. if suppose best method like:
return s1.tolowercase().contains(s2.tolowercase()); all aside, there (possibly better) way accomplish without caring case-sensitivity?
yes, contains case sensitive. can use java.util.regex.pattern case_insensitive flag case insensitive matching:
pattern.compile(pattern.quote(s2), pattern.case_insensitive).matcher(s1).find(); edit: if s2 contains regex special characters (of there many) it's important quote first. i've corrected answer since first 1 people see, vote matt quail's since pointed out.
Comments
Post a Comment