private static String[] generateNGrams(String str, int gramlength) {
        if (str == null || str.length() == 0) return null;
        ArrayList<String> grams = new ArrayList<String>();
        int length = str.length();
        String gram;
        if (length < gramlength) {
            for (int i = 1; i <= length; i++) {
                gram = str.substring(0, i);
                if (grams.indexOf(gram) == -1) grams.add(gram);
            }
            gram = str.substring(length - 1, length);
            if (grams.indexOf(gram) == -1) grams.add(gram);
        } else {
            for (int i = 1; i <= gramlength - 1; i++) {
                gram = str.substring(0, i);
                if (grams.indexOf(gram) == -1) grams.add(gram);
            }
            for (int i = 0; i < length - gramlength + 1; i++) {
                gram = str.substring(i, i + gramlength);
                if (grams.indexOf(gram) == -1) grams.add(gram);
            }
            for (int i = length - gramlength + 1; i < length; i++) {
                gram = str.substring(i, length);
                if (grams.indexOf(gram) == -1) grams.add(gram);
            }
        }
        return grams.toArray(new String[grams.size()]);
    }
private static String [ ] generateNGrams ( String str , int gramlength ) { if ( str == null || str . length ( ) == 0 ) return null ; ArrayList < String > grams = new ArrayList < String > ( ) ; int length = str . length ( ) ; String gram ; if ( length < gramlength ) { for ( int i = 1 ; i <= length ; i ++ ) { gram = str . substring ( 0 , i ) ; if ( grams . indexOf ( gram ) == - 1 ) grams . add ( gram ) ; } gram = str . substring ( length - 1 , length ) ; if ( grams . indexOf ( gram ) == - 1 ) grams . add ( gram ) ; } else { for ( int i = 1 ; i <= gramlength - 1 ; i ++ ) { gram = str . substring ( 0 , i ) ; if ( grams . indexOf ( gram ) == - 1 ) grams . add ( gram ) ; } for ( int i = 0 ; i < length - gramlength + 1 ; i ++ ) { gram = str . substring ( i , i + gramlength ) ; if ( grams . indexOf ( gram ) == - 1 ) grams . add ( gram ) ; } for ( int i = length - gramlength + 1 ; i < length ; i ++ ) { gram = str . substring ( i , length ) ; if ( grams . indexOf ( gram ) == - 1 ) grams . add ( gram ) ; } } return grams . toArray ( new String [ grams . size ( ) ] ) ; }
Produces nGrams for nGram matcher.

@param str        source string
@param gramlength gram length
@return ngrams