在Java1.4中,如何查看子字符串是否存在于另一个字符串中? 1 年,1 月 Questions & Answers 43543 如何判断子字符串“模板”(例如)是否存在于字符串对象中 如果不是区分大小写的检查,那就太好了
# 1 楼答案 public static boolean checkIfPasswordMatchUName(final String userName, final String passWd) { if (userName.isEmpty() || passWd.isEmpty() || passWd.length() > userName.length()) { return false; } int checkLength = 3; for (int outer = 0; (outer + checkLength) < passWd.length()+1; outer++) { String subPasswd = passWd.substring(outer, outer+checkLength); if(userName.contains(subPasswd)) { return true; } if(outer > (passWd.length()-checkLength)) break; } return false; }
# 2 楼答案 String.indexOf(String) 对于不区分大小写的搜索,在原始字符串和indexOf之前的子字符串上都使用toUpperCase或toLowerCase String full = "my template string"; String sub = "Template"; boolean fullContainsSub = full.toUpperCase().indexOf(sub.toUpperCase()) != -1;
# 3 楼答案 使用正则表达式并将其标记为不区分大小写: if (myStr.matches("(?i).*template.*")) { // whatever } (?i)打开大小写不敏感和*在搜索词的每一端匹配任何周围的字符(因为String.matches适用于整个字符串)
# 4 楼答案 String word = "cat"; String text = "The cat is on the table"; Boolean found; found = text.contains(word);
# 5 楼答案 您可以使用indexOf()和toLowerCase()对子字符串进行不区分大小写的测试 String string = "testword"; boolean containsTemplate = (string.toLowerCase().indexOf("template") >= 0);
# 1 楼答案
# 2 楼答案
String.indexOf(String)
对于不区分大小写的搜索,在原始字符串和indexOf之前的子字符串上都使用toUpperCase或toLowerCase
# 3 楼答案
使用正则表达式并将其标记为不区分大小写:
(?i)打开大小写不敏感和*在搜索词的每一端匹配任何周围的字符(因为String.matches适用于整个字符串)
# 4 楼答案
# 5 楼答案
您可以使用indexOf()和toLowerCase()对子字符串进行不区分大小写的测试