java统计字符串出现次数算法--StringCounter(算法源码)

时间:2013-02-03 13:12:05
【文件属性】:

文件名称:java统计字符串出现次数算法--StringCounter(算法源码)

文件大小:592B

文件格式:RAR

更新时间:2013-02-03 13:12:05

java,算法,字符串处理,统计字符串出现次数,算法源码

package com.hexiang.utils.tools; import java.util.TreeMap; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringCounter { /** * 正则统计字符串出现次数 * * @param source * @param regexNew * @return int */ public static int finder(String source, String regexNew) { String regex = "[a-zA-Z]+"; if (regexNew != null && !regexNew.equals("")) { regex = regexNew; } Pattern expression = Pattern.compile(regex); Matcher matcher = expression.matcher(source); TreeMap myTreeMap = new TreeMap(); int n = 0; Object word = null; Object num = null; while (matcher.find()) { word = matcher.group(); n++; if (myTreeMap.containsKey(word)) { num = myTreeMap.get(word); Integer count = (Integer) num; myTreeMap.put(word, new Integer(count.intValue() + 1)); } else { myTreeMap.put(word, new Integer(1)); } } return n; } }


【文件预览】:
StringCounter.java

网友评论

  • 这也叫算法?网上随便找都比你这好多了,还3个积分,。。醉了