import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class zifutongji {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str =new String("adsdfksjfhdlfdxzd+a-j");
String char_count=getCharCount(str);
System.out.println(char_count);
}
private static String getCharCount(String str) {
// TODO Auto-generated method stub
char[] chs=str.toCharArray();
Map<Character,Integer> map=new TreeMap<Character,Integer>();
for(int i=0;i<chs.length;i++)
{ if(!((chs[i]>='a' && chs[i]<='z') || (chs[i]>='A'&&chs[i]<='Z')))
continue;
int count =0;
Integer value=map.get(chs[i]);
if(!(value==null))
{
count=value;
}
count++;
map.put(chs[i], count);
}
return toString(map);
}
private static String toString(Map<Character, Integer> map) {
// TODO Auto-generated method stub
StringBuilder sb =new StringBuilder();
Set<Character> set = map.keySet();
for (Iterator<Character> iterator = set.iterator(); iterator.hasNext();) {
char s= iterator.next();
Integer key= map.get(s);
sb.append(s+"("+key+")");
}
return sb.toString();
}
}
统计字符串中字母出现的个数
最新推荐文章于 2021-12-25 23:01:08 发布