encode static method
Encodes this string using Soundex (first letter + 3 digits).
Non-alpha characters are ignored. Returns 4-char string (letter + 3 digits), or empty string if no letters.
Example:
SoundexUtils.encode('Robert'); // 'R163'
SoundexUtils.encode('Rupert'); // 'R163'
Implementation
static String encode(String s) {
if (s.isEmpty) return '';
final String letters = s.toUpperCase().replaceAll(RegExp(r'[^A-Z]'), '');
if (letters.isEmpty) return '';
final StringBuffer out = StringBuffer(letters[0]);
int prev = _code(letters[0]);
int count = 1;
for (int i = 1; i < letters.length && count < _soundexCodeLength; i++) {
final int c = _code(letters[i]);
if (c != 0 && c != prev) {
prev = c;
out.write(c);
count++;
}
}
while (count < _soundexCodeLength) {
out.write(_zero);
count++;
}
return out.toString();
}