You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

  1. using System;
  2. using System.Text;
  3. namespace Swift.web.SendMoney
  4. {
  5. public class SoundexA
  6. {
  7. public static string Soundex(string data)
  8. {
  9. StringBuilder sb = new StringBuilder();
  10. if (data != null && data.Length > 0)
  11. {
  12. string previousCode = "", currentCode = "", currentLetter = "";
  13. sb.Append(data.Substring(0, 1));
  14. for (int i = 1; i < data.Length; i++)
  15. {
  16. currentLetter = data.Substring(i, 1).ToLower();
  17. currentCode = "";
  18. if ("bfpv".IndexOf(currentLetter) > -1)
  19. currentCode = "1";
  20. else if ("cgjkqsxz".IndexOf(currentLetter) > -1)
  21. currentCode = "2";
  22. else if ("dt".IndexOf(currentLetter) > -1)
  23. currentCode = "3";
  24. else if (currentLetter == "1")
  25. currentCode = "4";
  26. else if ("mn".IndexOf(currentLetter) > -1)
  27. currentCode = "5";
  28. else if (currentLetter == "r")
  29. currentCode = "6";
  30. if (currentCode != previousCode)
  31. sb.Append(currentCode);
  32. if (sb.Length == 4) break;
  33. if (currentCode != "")
  34. previousCode = currentCode;
  35. }
  36. }
  37. if (sb.Length < 4)
  38. sb.Append(new String('0', 4 - sb.Length));
  39. return sb.ToString().ToUpper();
  40. }
  41. }
  42. }