public static String soundex ( String str ) { if ( str . length ( ) < 1 ) return "" ; // no soundex key for the empty string (could use 000) char [ ] key = new char [ 4 ] ; key [ 0 ] = str . charAt ( 0 ) ; int pos = 1 ; char prev = ' ' ; for ( int ix = 1 ; ix < str . length ( ) && pos < 4 ; ix ++ ) { char ch = str . charAt ( ix ) ; int charno ; if ( ch >= ' ' && ch <= ' ' ) charno = ch - ' ' ; else if ( ch >= ' ' && ch <= ' ' ) charno = ch - ' ' ; else continue ; if ( number [ charno ] != ' ' && number [ charno ] != prev ) key [ pos ++ ] = number [ charno ] ; prev = number [ charno ] ; } for ( ; pos < 4 ; pos ++ ) key [ pos ] = ' 0 ' ; return new String ( key ) ; }
Produces the Soundex key for the given string .