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

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

    import java.text.Normalizer;
    
    import org.apache.commons.lang3.StringUtils;
    
    public class ContainsIgnoreCase {
    
        public static void main(String[] args) {
    
            String in = "   Annulée ";
            String key = "annulee";
    
            // 100% java
            if (Normalizer.normalize(in, Normalizer.Form.NFD).replaceAll("[\\p{InCombiningDiacriticalMarks}]", "").toLowerCase().contains(key)) {
                System.out.println("OK");
            } else {
                System.out.println("KO");
            }
    
            // use commons.lang lib
            if (StringUtils.containsIgnoreCase(Normalizer.normalize(in, Normalizer.Form.NFD).replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""), key)) {
                System.out.println("OK");
            } else {
                System.out.println("KO");
            }
    
        }
    
    }
    

提交回复
热议问题