Soundex c#试图获得编码的第一位数

时间:2015-11-04 01:31:52

标签: c# .net visual-studio

正如你所看到的,我已将我的价值观设定为" SMITH"和" SMYTHE"在我的主要方法中。此值的输出应为25030,但由于某种原因,它的编码为250300.我认为这是因为它在该单词的第一个字符之前进行编码。例如SMITH是" S"所以这是编码作为" S"的第一个字符。如何使S成为数字或值?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoundDexFinal
{
    class Program
    {
        static void Main(string[] args)
        {

            string value1 = "SMITH";
            string value2 = "Smythe";

            soundex soundex = new soundex();
            Console.WriteLine(soundex.GetSoundex(value1));      // Outputs "S50300"
            Console.WriteLine(soundex.GetSoundex(value2));      // Outputs "S530"
            Console.WriteLine(soundex.Compare(value1, value2)); // Outputs "4"
            Console.ReadLine();
        }
    }

        namespace SoundDexFinal
    {
        class soundex
        {
            public string GetSoundex(string value)
            {
                value = value.ToUpper();
                StringBuilder soundex = new StringBuilder();
                foreach (char ch in value)
                {
                    if (char.IsLetter(ch))
                        AddCharacter(soundex, ch);

                }
                RemovePlaceholders(soundex);
                FixLength(soundex);
                return soundex.ToString();

            }


            private void AddCharacter(StringBuilder soundex, char ch)
            {
                if (soundex.Length == 0)
                    soundex.Append(ch);
                else
                {
                    string code = GetSoundexDigit(ch);
                    if (code != soundex[soundex.Length - 1].ToString())
                        soundex.Append(code);
                }
            }

            private string GetSoundexDigit(char ch)
            {
                string chString = ch.ToString();

                if ("AEIOUHWY".Contains(chString))
                    return "0";
                else if ("BFPV".Contains(chString))
                    return "1";
                else if ("CGJKQSXZ".Contains(chString))
                    return "2";
                else if ("DT".Contains(chString))
                    return "3";
                else if (ch == 'L')
                    return "4";
                else if ("MN".Contains(chString))
                    return "5";
                else if ("R".Contains(chString))
                    return "6";
                else
                    return ".";
            }

            private void RemovePlaceholders(StringBuilder soundex)
            {
                soundex.Replace(".", "");
            }

            private void FixLength(StringBuilder soundex)
            {
                int length = soundex.Length;
                if (length < 6)
                    soundex.Append(new string('0', 6 - length));
                else
                    soundex.Length = 6;
            }

            public int Compare(string value1, string value2)
            {
                int matches = 0;
                string soundex1 = GetSoundex(value1);
                string soundex2 = GetSoundex(value2);

                for (int i = 0; i < 6; i++)
                    if (soundex1[i] == soundex2[i]) matches++;

                return matches;
            }
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

您正在调用FixLength函数,如果字符串长度小于6,该函数会在字符串末尾附加额外的'0'。

这就是你得到“250300”而不是“25030”

的原因

答案 1 :(得分:0)

根据讨论,如此更改AddCharacter方法将实现您的目标:

private void AddCharacter(StringBuilder soundex, char ch)
{
    string code = GetSoundexDigit(ch);
    if (soundex.Length == 0 || code != soundex[soundex.Length - 1].ToString())
        soundex.Append(code);
}

但我不再提到“soundex”,因为它不再是。