static private int numOccurencesOfCharIn ( String str , char ch ) { boolean inMiddleOfQuote = false ; int num = 0 , previousIndex = 0 ; for ( int index = str . indexOf ( ch ) ; index >= 0 ; index = str . indexOf ( ch , index + 1 ) ) { if ( hasOddNumberOfSingleQuotes ( str . substring ( previousIndex , index ) ) ) { inMiddleOfQuote = ! inMiddleOfQuote ; } if ( ! inMiddleOfQuote ) { num ++ ; } previousIndex = index ; } return num ; }
Returns the number of occurrences of the specified character in the specified String but ignoring those contained in single quotes .