去除字串中所有的非英文字元,再統計每種英文字元的個數(不區分大小寫)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestString {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("please enter a string");
String str = in.next();
String regEx = "[^a-zA-Z]";
//create a regular expression
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(str);
String str2 = matcher.replaceAll("");
int[] a = new int[26];
int j = 0;
for (char ch='a';ch <='z';ch++)
{
for (int i=0;i<str2.length();i++)
{
if ((ch == str2.charAt(i)) || ((ch-32)==str2.charAt(i))) a[j]++;
}
j++;
}
j=0;
for (char ch='a';ch <='z';ch++)
{
System.out.println(ch+"\\"+(char)(ch-32)+"'s number is "+a[j]);
j++;
}
}
}