我正在創建一個循環,測試用戶輸入的每個子字符串以測試非法字符。但是,它僅檢測非法字符何時位於輸入的最後位置。我感覺好像解決方案可能很簡單,但我似乎無法弄清楚,我該如何糾正這個問題,以便它可以在字符串中的任何位置檢測到它?只有在java中測試循環的最後一個子字符串
public class IllegalFileName {
public static final Set<String> illegalCharsSet = new HashSet<String>(
Arrays.asList("/", "\n", "\r", "\t", "\0", "\f", "`", "?", "*",
"\\", "<", ">", "|", "\"", ":", ".", "£", "$", "%", "^",
"&", ")", "("));
public static void main(String[] args) {
String illegalNameText = "££!233";
String illegalNameText2 = "££!233$";
boolean illegalName = false;
for (int i = 0; i <= illegalNameText.length() - 1; i++) {
System.out.println(i);
if (illegalCharsSet.contains(illegalNameText.substring(i))) {
System.out.println(illegalNameText + " is illegal");
illegalName = true;
System.out.println(illegalNameText.substring(i));
}
}
for (int i = 0; i <= illegalNameText2.length() - 1; i++) {
System.out.println(i);
if (illegalCharsSet.contains(illegalNameText2.substring(i))) {
System.out.println(illegalNameText2 + " is illegal");
illegalName = true;
System.out.println(illegalNameText2.substring(i));
}
}
}
}
我現在看到我的錯誤,我一定誤解了API。謝謝! – 2013-03-18 16:01:08