import java.util.*;
public class demo {
public static void main(String args[]) throws Exception{
HashMapmap = new HashMap ();
String s = "abcdefgabc";
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
Integer val = map.get(new Character(c));
if(val != null){
map.put(c, new Integer(val + 1));
}else{
map.put(c,1);
}
}
printMap(map);
}
private static void printMap(Map map){ // 打印统计信息
Iterator it=map.entrySet().iterator();
while(it.hasNext()){
Map.Entry pair=(Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
}
}
判断字符串特定字符个数的方法是先用正则表达式提取,然后用lenth方法获取。
1、定义要处理的字符串:
var txt = "#div-name-1234-characteristic:561613213213";
2、需要从上面的txt中提取数字,写正则表达式:
var pattern = "/\d/g";
3、开始匹配并提取,赋值给numb:
var numb = txt.match(pattern);
4、连续提取的数字用join连接
numb = numb.join("");
5、打印结果如下:
1234561613213213