public static int countSubstrings(String string,String sub){
  int count=0;
  int index=-1;
  while ((index=string.indexOf(sub,index + 1)) != -1) {
    ++count;
  }
  return count;
}

public static int countSubstrings ( String string , String sub ) { int count = 0 ; int index = - 1 ; while ( ( index = string . indexOf ( sub , index + 1 ) ) != - 1 ) { ++ count ; } return count ; }
counts the number of substring in a string
