What's the best way to go about checking if a String being searched for is contained within another String (ignoring case).
ex) Cat is contained in Catastraphy
I searched the string class online but found no functions that might help me do this. Maybe just using indexOf() with a string parameter, because that way if I don't get back an index it's not there, and if I do it is there? I'm trying to keep a record of which strings contain other strings, like all the strings in a list that contain the word "Cat" for example. Thanks
Using indexOf() this way is common and a fine thing to do; but it will only find exact matches, including letter case. A more general solution would be to use regular expressions -- String.matches() or its more general form java.util.regex.Pattern.matches(). Something like
// Keep "p", use for all your Strings to check if they contain "cat" Pattern p = Pattern.compile(".*cat.*", Pattern.CASE_INSENSITIVE);
// For each string you want to match Matcher m = p.matcher(theString); boolean containsCatIgnoringCase = m.matches();