统计标点符号个数
String str = ""”.。,!,"; int count = 0; Pattern pattern = Pattern.compile("\pP"); char c[] = str.toCharArray(); for (int i = 0; i < c.length; i++) { Matcher matcher = pattern.matcher(String.valueOf(c[i])); if (matcher.matches()) { count++; } } System.out.println(count);
PS:如果在JDK5或一下的环境中,全角单引号对、双引号对无法替换掉。
在JDK6一下的可以这样:
Pattern pattern = Pattern.compile("\pP‘’“”");