- Apache Common StringUtils function
1: public static int countMatches(String str, String sub) {
2: if (isEmpty(str) || isEmpty(sub)) {
3: return 0;
4: }
5: int count = 0;
6: int idx = 0;
7: while ((idx = str.indexOf(sub, idx)) != -1) {
8: count++;
9: idx += sub.length();
10: }
11: return count;
12: }
13: public static boolean isEmpty(String str) {
14: return str == null || str.length() == 0;
15: }
1: public static int countMatchesWithRegex(String str, String reg) {
2: Matcher m = Pattern.compile(reg).matcher(str);
3: int matches = 0;
4: while(m.find())
5: matches++;
6: return matches;
7: }
- Difference: Regex approach can get correct counting over "Africa" and "African"
1: String target = "South Africa South African South Africa.";
2: System.out.println(StringUtils.countMatches(target, "South Africa")); // Return 3
3: System.out.println(StringUtils.countMatchesWithRegex(target, "\\bSouth African\\b")); // Return 1
No comments:
Post a Comment