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

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

    You can use

    org.apache.commons.lang3.StringUtils.containsIgnoreCase("AbBaCca", "bac");
    

    The Apache Commons library is very useful for this sort of thing. And this particular one may be better than regular expressions as regex is always expensive in terms of performance.

    0 讨论(0)
  • 2020-11-22 04:17

    You could simply do something like this:

    String s1 = "AbBaCca";
    String s2 = "bac";
    String toLower = s1.toLowerCase();
    return toLower.contains(s2);
    
    0 讨论(0)
  • 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!");
    }
    
    0 讨论(0)
  • 2020-11-22 04:20

    If you have to search an ASCII string in another ASCII string, such as a URL, you will find my solution to be better. I've tested icza's method and mine for the speed and here are the results:

    • Case 1 took 2788 ms - regionMatches
    • Case 2 took 1520 ms - my

    The code:

    public static String lowerCaseAscii(String s) {
        if (s == null)
            return null;
    
        int len = s.length();
        char[] buf = new char[len];
        s.getChars(0, len, buf, 0);
        for (int i=0; i<len; i++) {
            if (buf[i] >= 'A' && buf[i] <= 'Z')
                buf[i] += 0x20;
        }
    
        return new String(buf);
    }
    
    public static boolean containsIgnoreCaseAscii(String str, String searchStr) {
        return StringUtils.contains(lowerCaseAscii(str), lowerCaseAscii(searchStr));
    }
    
    0 讨论(0)
  • 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");
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 04:26

    Here's some Unicode-friendly ones you can make if you pull in ICU4j. I guess "ignore case" is questionable for the method names because although primary strength comparisons do ignore case, it's described as the specifics being locale-dependent. But it's hopefully locale-dependent in a way the user would expect.

    public static boolean containsIgnoreCase(String haystack, String needle) {
        return indexOfIgnoreCase(haystack, needle) >= 0;
    }
    
    public static int indexOfIgnoreCase(String haystack, String needle) {
        StringSearch stringSearch = new StringSearch(needle, haystack);
        stringSearch.getCollator().setStrength(Collator.PRIMARY);
        return stringSearch.first();
    }
    
    0 讨论(0)
提交回复
热议问题