How to check if a String contains another String in a case insensitive manner in Java?

前端 未结 19 1460
渐次进展
渐次进展 2020-11-22 03:20

Say I have two strings,

String s1 = \"AbBaCca\";
String s2 = \"bac\";

I want to perform a check returning that s2 is contained

19条回答
  •  再見小時候
    2020-11-22 04:13

    One problem with the answer by Dave L. is when s2 contains regex markup such as \d, etc.

    You want to call Pattern.quote() on s2:

    Pattern.compile(Pattern.quote(s2), Pattern.CASE_INSENSITIVE).matcher(s1).find();
    

提交回复
热议问题