15 lines
575 B
Java
15 lines
575 B
Java
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"));
|
|
}
|
|
}
|