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

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

    Yes, this is achievable:

    String s1 = "abBaCca";
    String s2 = "bac";
    
    String s1Lower = s1;
    
    //s1Lower is exact same string, now convert it to lowercase, I left the s1 intact for print purposes if needed
    
    s1Lower = s1Lower.toLowerCase();
    
    String trueStatement = "FALSE!";
    if (s1Lower.contains(s2)) {
    
        //THIS statement will be TRUE
        trueStatement = "TRUE!"
    }
    
    return trueStatement;
    

    This code will return the String "TRUE!" as it found that your characters were contained.

提交回复
热议问题