Skip to content

Text 0.4.0 metaphone encode

github-actions[bot] edited this page Aug 21, 2026 · 1 revision

Lodestar.Text 0.4.0. This page is frozen at that release. Read the current documentation for what main says now. A link to a decision or a migration page follows main, and leaves the archive.

Metaphone.Encode

The Metaphone 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.

Returnsstring, a variable-length code drawn from B X S K J T F H L M N P R 0 W Y, or the empty string when value holds no letter.

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 — the digraphs that make the alphabet look strange.

using Lodestar.Text.Phonetics;

string th = Metaphone.Encode("Thomas");  // => 0MS
string sh = Metaphone.Encode("Christina");  // => XRSTN
string silent = Metaphone.Encode("Knight");  // => NT

Remarks0 and X are not placeholders. 0 is the "th" sound and X the "sh" sound, both written as single characters so a code stays one character per sound. This is why a Metaphone code must never be shown to a user: 0MS is correct and unreadable.

KnightNT is the behaviour that distinguishes this from the other two encoders, and it is also the reason to prefer it for ordinary English words over Soundex.Encode. Silent k, w, g and b are all handled, along with GH, DGE, -TION and the rest of the spellings English uses for sounds it does not write plainly.

Parity is claimed on real words only. The shared 402-word corpus contains random letter sequences, and on those jellyfish behaves in ways specific to its C implementation; decision 0007 is the reasoning, and the practical form of it is that a code for xhdzhumzj is not something this package promises to match.

Applies to — net10.0, netstandard2.0.

See alsoMetaphone, Soundex.Encode, the phonetics index.

Lodestar

Project

Clone this wiki locally