【发布时间】:2015-01-10 10:16:34
【问题描述】:
我正在尝试制作一个程序来计算给定字符串中的特殊字符。但它不起作用。这是我的代码。
public static final String []specialChars = {"!", "@", "#", "$", "%", "^", "&", "*", "(", ")",
"[", "]", "|", ";", "'", ",", ".", "/", "{", "}",
"\\", ":", "\"", "<", ">", "?" };
我使用数组是因为我试图在字符串中跟踪匹配的特殊字符。所以我决定做循环。
int specialCharCount = 0;
for ( int x = 0; x < specialChars.length ; ++x) {
specialCharCount = password.length() - password.replaceAll("\\specialChars[x]", "").length();
}
System.out.print(" and " + specialCharCount + " special characters.\n\n");
它正在运行,但它给了我这个输出:
and 0 special characters.
【问题讨论】:
标签: java arrays string character