如何查看Java 1.4中另一个字符串中是否存在子字符串?
如何判断字符串对象中是否存在子字符串“template”(例如)?
如果它不是一个区分大小写的检查,那就太好了。
使用正则表达式并将其标记为不区分大小写:
if (myStr.matches("(?i).*template.*")) { // whatever }
(?i)打开不区分大小写,搜索项每端的。*匹配任何周围的字符(因为String.matches适用于整个字符串)。
String.indexOf(字符串)
对于不区分大小写的搜索,在indexOf之前的原始字符串和子字符串上的toUpperCase或toLowerCase
String full = "my template string"; String sub = "Template"; boolean fullContainsSub = full.toUpperCase().indexOf(sub.toUpperCase()) != -1;
您可以使用indexOf()和toLowerCase()对子字符串执行不区分大小写的测试。
String string = "testword"; boolean containsTemplate = (string.toLowerCase().indexOf("template") >= 0);
String word = "cat"; String text = "The cat is on the table"; Boolean found; found = text.contains(word);