Skip to content

Text soundex encode

github-actions[bot] edited this page Aug 22, 2026 · 24 revisions

Soundex.Encode

The 4-character Soundex code of one word.

public static string Encode(ReadOnlySpan<char> value)
public static string Encode(string value)

Parametersvalue is a single word. Non-letters in it are ignored rather than rejected, and case does not matter. The string overload forwards to the span one, so passing a string allocates nothing extra.

Returnsstring: an uppercase letter followed by three digits, always exactly four characters — or the empty string when value holds no letter at all.

ExceptionsArgumentNullException when value is null (the string overload only; a ReadOnlySpan<char> cannot be null). An empty string is accepted and encodes to the empty string.

Example — a collision, and a word with too few consonants to fill the code.

using Lodestar.Text.Phonetics;

string robert = Soundex.Encode("Robert");  // => R163
string rupert = Soundex.Encode("Rupert");  // => R163
string padded = Soundex.Encode("Lee");  // => L000

RemarksL000 is the padding rule doing its work: Lee has one codeable consonant and the code is still four characters wide, because a fixed width is what lets Soundex be an index key.

A null word is refused, the same rule the stemmers in Lodestar.Text.Stemming apply — decision 0042 records why the two used to disagree and why refusing won.

The two overloads are the same algorithm; the span one exists so a word already sliced out of a larger buffer can be encoded without copying it.

Applies to — net10.0, netstandard2.0.

See alsoSoundex, Nysiis.Encode, the phonetics index.

Lodestar

Project

Clone this wiki locally