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

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

    We can use stream with anyMatch and contains of Java 8

    public class Test2 {
        public static void main(String[] args) {
    
            String a = "Gina Gini Protijayi Soudipta";
            String b = "Gini";
    
            System.out.println(WordPresentOrNot(a, b));
        }// main
    
        private static boolean WordPresentOrNot(String a, String b) {
        //contains is case sensitive. That's why change it to upper or lower case. Then check
            // Here we are using stream with anyMatch
            boolean match = Arrays.stream(a.toLowerCase().split(" ")).anyMatch(b.toLowerCase()::contains);
            return match;
        }
    
    }
    

提交回复
热议问题