privateintpattern(Stringstr,Stringreg){Patternpattern=Pattern.compile(reg);// Create a matcher for the input String
Matchermatcher=pattern.matcher(str);intcount=0;while(matcher.find()){count++;}returncount;}
Using indexOf() and substring and recuesion
1
2
3
4
5
6
7
8
9
10
11
privatevoidindex(Stringtext){intindexs=text.indexOf("cs");if(indexs!=-1){// means have substring
count++;if(indexs<text.length()-1){text=text.substring(indexs+1);index(text);}}}