Skip to content

Instantly share code, notes, and snippets.

@ashpriom
Last active August 29, 2015 14:06
Show Gist options
  • Save ashpriom/f3b1927f008fec5771ff to your computer and use it in GitHub Desktop.
Save ashpriom/f3b1927f008fec5771ff to your computer and use it in GitHub Desktop.

import java.util.regex.Pattern;

public class CountSubstring { public static int countSubstring(String subStr, String str){ // the result of split() will contain one more element than the delimiter // the “-1” second argument makes it not discard trailing empty strings return str.split(Pattern.quote(subStr), -1).length - 1; }

public static void main(String[] args){ System.out.println(countSubstring(“th”, “the three truths”)); System.out.println(countSubstring(“abab”, “ababababab”)); System.out.println(countSubstring(“a*b”, “abaabba*bbaba*bbab”)); } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment