I have a database table that has a column of SQLServer Soundex encoded last name + first name. In my C# program I would like to convert a string using soundex for use in my
Based on the answer of Dotnet Services and tigrou, I have corrected the algorithm in order to reflect the function described in Wikipedia.
Test cases such as Ashcraft = A226, Tymczak = T522, Pfister = P236 and Honeyman = H555 are now working correctly.
public static string Soundex(string data)
{
StringBuilder result = new StringBuilder();
if (data != null && data.Length > 0)
{
string previousCode = "", currentCode = "", currentLetter = "";
result.Append(data[0]); // keep initial char
for (int i = 0; i < data.Length; i++) //start at 0 in order to correctly encode "Pf..."
{
currentLetter = data[i].ToString().ToLower();
currentCode = "";
if ("bfpv".Contains(currentLetter))
currentCode = "1";
else if ("cgjkqsxz".Contains(currentLetter))
currentCode = "2";
else if ("dt".Contains(currentLetter))
currentCode = "3";
else if (currentLetter == "l")
currentCode = "4";
else if ("mn".Contains(currentLetter))
currentCode = "5";
else if (currentLetter == "r")
currentCode = "6";
if (currentCode != previousCode && i > 0) // do not add first code to result string
result.Append(currentCode);
if (result.Length == 4) break;
previousCode = currentCode; // always retain previous code, even empty
}
}
if (result.Length < 4)
result.Append(new String('0', 4 - result.Length));
return result.ToString().ToUpper();
}