StringUtil { public static int countMatches(String str, String subStr) { if ((str == null) || (str.length() == 0) || (subStr == null) || (subStr.length() == 0)) { return 0; } int count = 0; int index = 0; while ((index = str.indexOf(subStr, index)) != -1) { count++; index += subStr.length(); } return count; }  static boolean isEmpty(String str); static boolean isAllEmpty(String...strings); static boolean isAnyEmpty(String...strings); static boolean isNotEmpty(String str); static boolean isBlank(String str); static boolean isAllBlank(String...strings); static boolean isAnyBlank(String...strings); static boolean isNotBlank(String str); static String defaultIfNull(String str); static String defaultIfNull(String str, String defaultStr); static String defaultIfEmpty(String str); static String defaultIfEmpty(String str, String defaultStr); static String defaultIfBlank(String str); static String defaultIfBlank(String str, String defaultStr); static String trim(String str); static void trimAll(String[] strings); static String trim(String str, String stripChars); static String trimStart(String str); static String trimStart(String str, String stripChars); static String trimEnd(String str); static String trimEnd(String str, String stripChars); static String trimToNull(String str); static String trimToNull(String str, String stripChars); static String trimToEmpty(String str); static String trimToEmpty(String str, String stripChars); static boolean equals(String str1, String str2); static boolean equalsIgnoreCase(String str1, String str2); static int equalsOne(String src, String[] dest); static int equalsOneIgnoreCase(String src, String[] dest); static boolean equalsIgnoreCase(String as[], String as1[]); static boolean isAlpha(String str); static boolean isAlphaSpace(String str); static boolean isAlphanumeric(String str); static boolean isAlphanumericSpace(String str); static boolean isNumeric(String str); static boolean isNumericSpace(String str); static boolean isWhitespace(String str); static String toUpperCase(String str); static String toLowerCase(String str); static String capitalize(String str); static String uncapitalize(String str); static String decapitalize(String str); static String swapCase(String str); static String fromCamelCase(String str, char separator); static String[] split(String str); static List<Long> parseStringToLongList(String source, String token); static String[] splitNoCompress(String src, String delimiter); static String[] splitc(String src, String d); static String[] splitc(String src, char delimiter); static String[] splitc(String src, char[] delimiters); static String[] split(String str, char separatorChar); static String[] split(String str, String separatorChars); static String[] split(String str, String separatorChars, int max); static String join(T...array); static String join(T[] array, char separator); static String join(T[] array, String separator); static String join(Iterator<?> iterator, char separator); static String join(Iterator<?> iterator, String separator); static int indexOf(String str, char searchChar); static int indexOf(String str, char searchChar, int startPos); static int indexOf(String str, String searchStr); static int indexOf(String str, String searchStr, int startPos); static int indexOf(int num, String str, String searchStr); static int indexOfAny(String str, char[] searchChars); static int indexOfAny(String str, char[] searchChars, int startIndex); static int indexOfAny(String str, String searchChars); static int indexOfAny(String str, String searchChars, int startIndex); static int indexOfAny(String str, String[] searchStrs); static int indexOfAny(String str, String[] searchStrs, int startIndex); static int indexOfAnyBut(String str, char[] searchChars); static int indexOfAnyBut(String str, String searchChars); static int lastIndexOf(String str, char searchChar); static int lastIndexOf(String str, char searchChar, int startPos); static int lastIndexOf(String str, String searchStr); static int lastIndexOf(String str, String searchStr, int startPos); static int lastIndexOfAny(String str, String[] searchStrs); static boolean contains(String str, char searchChar); static boolean contains(String str, String searchStr); static boolean containsOnly(String str, char[] valid); static boolean containsOnly(String str, String valid); static boolean containsNone(String str, char[] invalid); static boolean containsNone(String str, String invalidChars); static int countMatches(String str, String subStr); static String substring(String str, int start); static String substring(String str, int start, int end); static String left(String str, int len); static String right(String str, int len); static String mid(String str, int pos, int len); static String substringBefore(String str, String separator); static String substringAfter(String str, String separator); static String substringBeforeLast(String str, String separator); static String substringAfterLast(String str, String separator); static String substringBetween(String str, String tag); static String substringBetween(String str, String open, String close); static String substringBetween(String str, String open, String close, int fromIndex); static String deleteWhitespace(String str); static String replaceOnce(String text, String repl, String with); static String replaceOnce(String str, char searchChar, char replaceChar); static String replace(String text, String repl, String with); static String replace(String text, String repl, String with, int max); static String replaceOnce(int startPos, String text, String repl, String with); static String replaceChars(String str, char searchChar, char replaceChar); static String replaceChars(String s, char[] sub, char[] with); static String replaceChars(String str, String searchChars, String replaceChars); static String replaceLast(String text, String sub, String with); static String replaceLast(String text, char sub, char with); static String overlay(String str, String overlay, int start, int end); static String chomp(String str); static String chomp(String str, String separator); static String chop(String str); static String repeat(String str, int repeat); static String repeat(char c, int count); static String alignLeft(String str, int size); static String alignLeft(String str, int size, char padChar); static String alignLeft(String str, int size, String padStr); static String alignRight(String str, int size); static String alignRight(String str, int size, char padChar); static String alignRight(String str, int size, String padStr); static String center(String str, int size); static String center(String str, int size, char padChar); static String center(String str, int size, String padStr); static String reverse(String str); static String reverseDelimited(String str, char separatorChar); static String reverseDelimited(String str, String separatorChars, String separator); static String abbreviate(String str, int maxWidth); static String abbreviate(String str, int offset, int maxWidth); static String difference(String str1, String str2); static int indexOfDifference(String str1, String str2); static int getLevenshteinDistance(String s, String t); static String STAM(String str, String wadChar, int length); static String toUpperCase4First(String str); static String toLowerCase4First(String str); static String getASCII(String str); static String toCamelCase(String str); static String toPascalCase(String str); static String toUpperCaseWithUnderscores(String str); static String toLowerCaseWithUnderscores(String str); static String longToString(long longValue); static String longToString(long longValue, boolean noCase); static String bytesToString(byte[] bytes); static String bytesToString(byte[] bytes, boolean noCase); static boolean endsWithChar(String str, char ch); static boolean startsWithChar(String str, char ch); static int indexOfChars(String string, String chars); static int indexOfChars(String string, String chars, int startindex); static int indexOfChars(String string, char[] chars); static int indexOfChars(String string, char[] chars, int startindex); static String toString(Object value); static String toString(Collection<String> collection); static String toString(Collection<String> collection, String split); static boolean isDigits(String string); static boolean endsWithIgnoreCase(String str, String suffix);  }
@Test public void countMatches() { assertEquals(0, StringUtil.countMatches(null, "a")); assertEquals(0, StringUtil.countMatches("", "a")); assertEquals(0, StringUtil.countMatches("abba", null)); assertEquals(0, StringUtil.countMatches("abba", "")); assertEquals(2, StringUtil.countMatches("abba", "a")); assertEquals(1, StringUtil.countMatches("abba", "ab")); assertEquals(0, StringUtil.countMatches("abba", "xxx")); assertEquals(3, StringUtil.countMatches("xxx{}yyy{},...zzz{}aaa", "{}")); }