第1个回答 推荐于2017-09-18
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.CyclicBarrier;
public class TestStrNumber {
private static String str1 = "sdfg,hellozx,sdfcv-waadfa,fsasdfxcvdf";
public static void main(String[] args) {
String ss=getUnmatchStr("hello");
getCharNum(ss);
}
public static void getCharNum(String ss){
String sb="";
for (int i = 0; i < ss.length(); i++) {
if (ss.charAt(i)!='-'&&ss.charAt(i)!=',') {
sb+=ss.charAt(i);
}
}
char[] cs=sb.toCharArray();
int oldNum=0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : cs) {
if (map.containsKey(c)) {
oldNum=map.get(c);
map.put(c, oldNum+1);
}else {
map.put(c, 1);
}
}
Set<Character> keys = map.keySet();
for(Character c:keys){
System.out.print(c+"(出现"+map.get(c)+"次)");
}
}
public static String getUnmatchStr(String s){
String str2="";
int startIndex=0;
int endIndex=0;
if (str1.contains(s)) {
startIndex = str1.indexOf(s);
endIndex=startIndex+s.length();
}
str2 = str1.substring(0, startIndex)+str1.substring(endIndex, str1.length());
return str2;
}
}本回答被提问者采纳
第2个回答 2013-04-16
public static void main(String[] args) {
String s = "abc a bc abc bc abc";
Map<String,Integer> map = new HashMap<String,Integer>();
for(String str : s.split("|")){
if(map.containsKey(str)){
map.put(str, map.get(str)+1);
}else{
map.put(str, 1);
}
}
for(Entry<String,Integer> e : map.entrySet()){
System.out.println(e.getKey() +": "+e.getValue()+"个");
}
}