日期:2014-05-20 浏览次数:20740 次
public static void main(String[] args) { String str = "中文fdas中ad文jak中jfsadfasdf"; char[] chars = str.toCharArray(); HashMap<String, Integer> m = new HashMap<String, Integer>(); for(int i = 0 ;i<chars.length;i++){ int count = m.get(""+chars[i]) == null?0:m.get(""+chars[i]); m.put(""+chars[i], ++count); } for(String key : m.keySet()){ System.out.println(key + ":" + m.get(key)); } }
------解决方案--------------------
public static void main(String[] args) { String a = "中文fdas中ad文jak中jfsadfasdf"; String[] arr = a.split("(?<=\\G.{1})"); String s2 = ""; Arrays.sort(arr); for (String i : arr) { s2 += i + "-"; } Matcher m = Pattern.compile("((\\w*-)|([\u4e00-\u9fa5]-))\\1*").matcher(s2); while (m.find()) { System.out.println(m.group().substring(0, 1) + "的个数:" + m.group().split("-").length); } }