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

前端 未结 19 1432
渐次进展
渐次进展 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:19

    A simpler way of doing this (without worrying about pattern matching) would be converting both Strings to lowercase:

    String foobar = "fooBar";
    String bar = "FOO";
    if (foobar.toLowerCase().contains(bar.toLowerCase()) {
        System.out.println("It's a match!");
    }
    

提交回复
热议问题