package
javastudy;
import
java.util.HashMap;
public
class CountLetters {
publicstaticvoid main ( String args[] )
{
String input =
"016a 8b9c213d20df0G9E";
String regex =
"(?i)[^a-z]";
String result = input.replaceAll (regex,
"");
System.
out.println (result);
HashMap<String, Integer> map =
new HashMap<String, Integer> ();
for ( int i = 0; i < result.length (); i++ )
{
String one = result.charAt (i) +
"";
if (null == map.get (one))
{
map.put (one, 1);
}
else
{
map.put (one, map.get (one) + 1);
}
}
System.
out.println (map);
countLetter();
}
publicstaticvoid countLetter(){
String str =
"另顶戴枯aslkdfjasf345sdfa";
char[] arr = str.toCharArray();
int number = 0;
int english = 0;
for (char c: arr)
{
int i = c + 0;
if (i > 47 && i < 58)
number++;
elseif ((i > 64 && i < 91) || (i > 96 && i < 123))
english++;
}
System.
out.println("原字符串:"+str+"数字:"+number +"个,"+"字母:"+english+ "个");
}
}
输出结果:
abcddfGE
{f=1, E=1, d=2, G=1, b=1, c=1, a=1}
原字符串:另顶戴枯aslkdfjasf345sdfa数字:3个,字母:14个