ITeye博客

正则表达式统计字符串出现次数

gangwu · 2009-05-27 17:14 · J2SE · 正则表达式 · Markdown

	public static void main(String[] args) {
		String str = "中国,美国,日本,朝鲜,中国,中国,美国,越南";
		String reg = "中国";
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(str);
		int i = 0;
		
		while(m.find()) {
			i++;
		}
		
		System.out.println(i);
	}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。