【发布时间】:2015-07-02 03:59:26
【问题描述】:
我正在尝试使用通配符生成一个单词并检查该单词是否存储在字典数据库中。像“appl*”应该返回apply或apple。但是,当我有 2 张通配符时,问题就来了。 "app**" 将生成 appaa、appbb..appzz... 之类的词,而不是 apple。第二个 if 条件仅适用于不包含通配符的常规字符串“*”
public static boolean printWords(String s) {
String tempString, tempChar;
if (s.contains("*")) {
for (char c = 'a'; c <= 'z'; c++) {
tempChar = Character.toString(c);
tempString = s.replace("*", tempChar);
if (myDictionary.containsKey(tempString) == true) {
System.out.println(tempString);
}
}
}
if (myDictionary.containsKey(s) == true) {
System.out.println(s);
return true;
} else {
return false;
}
}
【问题讨论】:
-
您应该为此使用正则表达式:docs.oracle.com/javase/tutorial/essential/regex