public class daytest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="Welcome to Chinaworld";
StringBuilder sb = getCharNum(str);
System.out.println(sb);
}
public static StringBuilder getCharNum(String str) {
//TreeMap可以排序
Map<String,Integer> map=new TreeMap<String,Integer>();
for(int i=0;i<str.length();i++)
{
int itc=str.charAt(i);
//只要字母,大小写转换。
if(itc>'z' || (itc<'a'&& itc>'z') || itc<'A' )
continue;
if(itc>97 && itc<122 && map.containsKey(Character.toString((char)(itc-32))))
itc-=32;
if(itc>65 && itc<87 && map.containsKey(Character.toString((char)(itc+32))))
itc+=32;
String s=Character.toString((char)itc);
int num=0;
if(map.containsKey(s))
num=map.get(s);
num++;
//相同Key时,只修改Values,不新增。当key不时才新增。
map.put(s, num);
}
StringBuilder sb=new StringBuilder();
for(Map.Entry<String, Integer> entery:map.entrySet())
{
sb.append("\""+entery.getKey()+entery.getValue()+"\"," );
}
if(sb.length()>0)
sb=sb.deleteCharAt(sb.length()-1);
return sb;
}
}
运行结果 "W2","a1","c2","d1","e2","h1","i1","l2","m1","n1","o3","r1","t1" |