List<String> cursewords = new ArrayList<String>();
cursewords.add("darn it");
cursewords.add("gosh");
cursewords.add("gee whiz");
cursewords.add("golly");
String text = " Golly ";
if (cursewords.contains(text.trim().toLowerCase()) {
System.out.println("found curse:" + text);
}
Is there a better way to do this?
My filter is not catching things it needs to.
Your filter will currently only work if
textis identical to one of the entries incursewords(with no other characters at all). To fix it, you need to instead iterate through the items incursewordsand check to see iftextcontains it.Here’s a simple example (uses an enhanced
forloop):Although as others have mentioned, it would probably be better to use regular expressions to account for variations in curses and avoid clbuttic mistakes.