public static int substr_count ( final String string , final String substring ) { if ( substring . length ( ) == 1 ) { return substr_count ( string , substring . charAt ( 0 ) ) ; } int count = 0 ; int idx = 0 ; while ( ( idx = string . indexOf ( substring , idx ) ) != - 1 ) { ++ idx ; ++ count ; } return count ; }
Count the number of substring occurrences .