How can I tell if the substring "template" exists inside a String in java 1.4
It would be great if it was a non case sensitive check.
Thanks!
How can I tell if the substring "template" exists inside a String in java 1.4
It would be great if it was a non case sensitive check.
Thanks!
For a case insensitive search, to toUpperCase or toLowerCase on both the original string and the substring before the indexOf
String full = "my template string";
String sub = "Template";
boolean fullContainsSub = full.toUpperCase().indexOf(sub.toUpperCase()) != -1;
You can use indexOf() and toLowerCase() to do case-insensitive tests for substrings.
String string = "testword";
boolean containsTemplate = (string.toLowerCase().indexOf("template") >= 0);
Use a regular expression and mark it as case insensitive:
if (myStr.matches("(?i).*template.*")) {
// whatever
}
The (?i) turns on case insensitivity and the .* at each end of the search term match any surrounding characters (since String.matches works on the entire string).