15

Is there a way to check if a string contains something while not being case sensitive?

For example: (this code is invalid it's just for you to get a basic understanding of my question)

String text = "I love ponies";

if(text.contains().equalsIgnoreCase("love") {
    // do something
}

EDIT: -------- Still not working

ooh, turns out it's not working. Here's what I'm using. (it's a curse filter for a game)

public void onChat(PlayerChatEvent event) {
    Player player = event.getPlayer(); 
    if (event.getMessage().contains("douche".toLowerCase()) || /* More words ... */) {
        event.setCancelled(true);
        player.sendMessage(ChatColor.GOLD + "[Midnight Blue] " + ChatColor.RED + "Please Don't Swear.");
    }
}

It works with lowercase but not uppercase.

Jan Bluemink
  • 3,467
  • 1
  • 21
  • 35
Hayden Taylor
  • 329
  • 1
  • 4
  • 9
  • 1
    possible duplicate of [Is the Contains Method in java.lang.String Case-sensitive?](http://stackoverflow.com/questions/86780/is-the-contains-method-in-java-lang-string-case-sensitive) – Cat Feb 20 '13 at 04:29
  • It's still not working because you havn't read the answers properly. If you want to use that style then read Anubhooti Pareek's answer again. You need event.getMessage().toLowerCase().contains(otherString.toLowerCase()) – David Saxon Feb 20 '13 at 05:08
  • Also if you plan to filter out all bad words with them hard coded into an if statement you are going to end up with a very big conditional... – David Saxon Feb 20 '13 at 05:09

4 Answers4

28
return text.toLowerCase().contains(s2.toLowerCase());

Or another way would be

Pattern.compile(Pattern.quote(s2), Pattern.CASE_INSENSITIVE).matcher(text).find();
Cat
  • 66,919
  • 24
  • 133
  • 141
8

It would be easier if you use StringUtils#containsIgnoreCase from Apache Commons library

If you can't add a third party library, you can still use the code because is free to use. Check the online source code.

Test:

public class QuestionABCD {
    public static boolean containsIgnoreCase(String str, String searchStr) {
        if (str == null || searchStr == null) {
            return false;
        }
        int len = searchStr.length();
        int max = str.length() - len;
        for (int i = 0; i <= max; i++) {
            if (str.regionMatches(true, i, searchStr, 0, len)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(containsIgnoreCase("abc", "A"));
        System.out.println(containsIgnoreCase("abc", "a"));
        System.out.println(containsIgnoreCase("abc", "B"));
        System.out.println(containsIgnoreCase("abc", "b"));
        System.out.println(containsIgnoreCase("abc", "z"));
        System.out.println(containsIgnoreCase("abc", "Z"));
    }
}

Output:

true
true
true
true
false
false
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 3
    +1 for the alternative you have given if the OP does not want to add the third party library :) – LGAP Feb 20 '13 at 04:43
7

If case sensitivity is your only issue convert everything into lowercase

String text = "I love ponies";
String test = "LOVE";
if(text.toLowerCase().contains(test.toLowerCase()))
{
//your code
}

update: for your code use :

event.getMessage().toLowerCase().contains("douche".toLowerCase())

in all the conditions

Shurmajee
  • 1,027
  • 3
  • 12
  • 35
-8

You can check twice like this

text.contains(s);
text.contains(s.toLowerCase());
chopchop
  • 1,905
  • 2
  • 22
  • 37