@Test(dataProvider = "testCaseGenerator") public void testCountMatches(String str, String subStr) throws Exception { Assert.assertEquals( StringTools.countMatches(str, subStr), StringTools.countMatches(str, subStr), "test failed: \nstr=\"" + str + "\"\nsubStr=\"" + subStr + "\"" ); }
public static int countMatches(CharSequence sourceSequence, CharSequence subSequence) { if (isEmpty(sourceSequence) || isEmpty(subSequence) || sourceSequence.length() < subSequence.length()) { return 0; } int count = 0; int sourceSequenceIndex = 0; int subSequenceIndex = 0; while(sourceSequenceIndex < sourceSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) == subSequence.charAt(subSequenceIndex)) { sourceSequenceIndex++; subSequenceIndex++; while(sourceSequenceIndex < sourceSequence.length() && subSequenceIndex < subSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) != subSequence.charAt(subSequenceIndex)) { break; } sourceSequenceIndex++; subSequenceIndex++; } if(subSequenceIndex == subSequence.length()) { count++; } subSequenceIndex = 0; continue; } sourceSequenceIndex++; } return count; }
StringTools { public static int countMatches(CharSequence sourceSequence, CharSequence subSequence) { if (isEmpty(sourceSequence) || isEmpty(subSequence) || sourceSequence.length() < subSequence.length()) { return 0; } int count = 0; int sourceSequenceIndex = 0; int subSequenceIndex = 0; while(sourceSequenceIndex < sourceSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) == subSequence.charAt(subSequenceIndex)) { sourceSequenceIndex++; subSequenceIndex++; while(sourceSequenceIndex < sourceSequence.length() && subSequenceIndex < subSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) != subSequence.charAt(subSequenceIndex)) { break; } sourceSequenceIndex++; subSequenceIndex++; } if(subSequenceIndex == subSequence.length()) { count++; } subSequenceIndex = 0; continue; } sourceSequenceIndex++; } return count; } }
StringTools { public static int countMatches(CharSequence sourceSequence, CharSequence subSequence) { if (isEmpty(sourceSequence) || isEmpty(subSequence) || sourceSequence.length() < subSequence.length()) { return 0; } int count = 0; int sourceSequenceIndex = 0; int subSequenceIndex = 0; while(sourceSequenceIndex < sourceSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) == subSequence.charAt(subSequenceIndex)) { sourceSequenceIndex++; subSequenceIndex++; while(sourceSequenceIndex < sourceSequence.length() && subSequenceIndex < subSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) != subSequence.charAt(subSequenceIndex)) { break; } sourceSequenceIndex++; subSequenceIndex++; } if(subSequenceIndex == subSequence.length()) { count++; } subSequenceIndex = 0; continue; } sourceSequenceIndex++; } return count; }  }
StringTools { public static int countMatches(CharSequence sourceSequence, CharSequence subSequence) { if (isEmpty(sourceSequence) || isEmpty(subSequence) || sourceSequence.length() < subSequence.length()) { return 0; } int count = 0; int sourceSequenceIndex = 0; int subSequenceIndex = 0; while(sourceSequenceIndex < sourceSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) == subSequence.charAt(subSequenceIndex)) { sourceSequenceIndex++; subSequenceIndex++; while(sourceSequenceIndex < sourceSequence.length() && subSequenceIndex < subSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) != subSequence.charAt(subSequenceIndex)) { break; } sourceSequenceIndex++; subSequenceIndex++; } if(subSequenceIndex == subSequence.length()) { count++; } subSequenceIndex = 0; continue; } sourceSequenceIndex++; } return count; }  static int countMatches(CharSequence sourceSequence, CharSequence subSequence); static boolean isNotBlank(CharSequence sourceSequence); static boolean isBlank(CharSequence sourceSequence); static boolean isEmpty(CharSequence sourceSequence); }
StringTools { public static int countMatches(CharSequence sourceSequence, CharSequence subSequence) { if (isEmpty(sourceSequence) || isEmpty(subSequence) || sourceSequence.length() < subSequence.length()) { return 0; } int count = 0; int sourceSequenceIndex = 0; int subSequenceIndex = 0; while(sourceSequenceIndex < sourceSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) == subSequence.charAt(subSequenceIndex)) { sourceSequenceIndex++; subSequenceIndex++; while(sourceSequenceIndex < sourceSequence.length() && subSequenceIndex < subSequence.length()) { if(sourceSequence.charAt(sourceSequenceIndex) != subSequence.charAt(subSequenceIndex)) { break; } sourceSequenceIndex++; subSequenceIndex++; } if(subSequenceIndex == subSequence.length()) { count++; } subSequenceIndex = 0; continue; } sourceSequenceIndex++; } return count; }  static int countMatches(CharSequence sourceSequence, CharSequence subSequence); static boolean isNotBlank(CharSequence sourceSequence); static boolean isBlank(CharSequence sourceSequence); static boolean isEmpty(CharSequence sourceSequence);  }