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

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

    There is a simple concise way, using regex flag (case insensitive {i}):

     String s1 = "hello abc efg";
     String s2 = "ABC";
     s1.matches(".*(?i)"+s2+".*");
    
    /*
     * .*  denotes every character except line break
     * (?i) denotes case insensitivity flag enabled for s2 (String)
     * */
    

提交回复
热议问题