PingYinHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Microsoft.International.Converters.PinYinConverter;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using NUnit.Framework.Internal;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. namespace TEAMModelOS.SDK
  8. {
  9. public static class PingYinHelper
  10. {
  11. /// <summary>
  12. /// 汉字转全拼
  13. /// </summary>
  14. /// <param name="strChinese"></param>
  15. /// <returns></returns>
  16. public static string ConvertToAllSpell(string strChinese)
  17. {
  18. try
  19. {
  20. if (strChinese.Length != 0)
  21. {
  22. StringBuilder fullSpell = new StringBuilder();
  23. for (int i = 0; i < strChinese.Length; i++)
  24. {
  25. var chr = strChinese[i];
  26. fullSpell.Append(GetSpell(chr));
  27. }
  28. return fullSpell.ToString().ToLower();
  29. }
  30. }
  31. catch (Exception e)
  32. {
  33. }
  34. return string.Empty;
  35. }
  36. /// <summary>
  37. /// 汉字转首字母
  38. /// </summary>
  39. /// <param name="strChinese"></param>
  40. /// <returns></returns>
  41. public static string GetFirstSpell(string strChinese)
  42. {
  43. //NPinyin.Pinyin.GetInitials(strChinese) 有Bug 洺无法识别
  44. //return NPinyin.Pinyin.GetInitials(strChinese);
  45. try
  46. {
  47. if (strChinese.Length != 0)
  48. {
  49. StringBuilder fullSpell = new StringBuilder();
  50. for (int i = 0; i < strChinese.Length; i++)
  51. {
  52. var chr = strChinese[i];
  53. fullSpell.Append(GetSpell(chr)[0]);
  54. }
  55. return fullSpell.ToString().ToLower();
  56. }
  57. }
  58. catch (Exception e)
  59. {
  60. }
  61. return string.Empty;
  62. }
  63. public static string GetSecondSpell(string strChinese)
  64. {
  65. //NPinyin.Pinyin.GetInitials(strChinese) 有Bug 洺无法识别
  66. //return NPinyin.Pinyin.GetInitials(strChinese);
  67. try
  68. {
  69. if (strChinese.Length != 0)
  70. {
  71. StringBuilder fullSpell = new StringBuilder();
  72. for (int i = 0; i < strChinese.Length; i++)
  73. {
  74. var chr = strChinese[i];
  75. var a = GetSpell(chr);
  76. if (a.Length > 1)
  77. {
  78. fullSpell.Append(a[1]);
  79. }
  80. else {
  81. fullSpell.Append(a[0]);
  82. }
  83. }
  84. return fullSpell.ToString().ToLower();
  85. }
  86. }
  87. catch (Exception e)
  88. {
  89. }
  90. return string.Empty;
  91. }
  92. public static string GetSpell(char chr)
  93. {
  94. var coverchr = NPinyin.Pinyin.GetPinyin(chr);
  95. bool isChineses = ChineseChar.IsValidChar(coverchr[0]);
  96. if (isChineses)
  97. {
  98. ChineseChar chineseChar = new ChineseChar(coverchr[0]);
  99. foreach (string value in chineseChar.Pinyins)
  100. {
  101. if (!string.IsNullOrEmpty(value))
  102. {
  103. return value.Remove(value.Length - 1, 1);
  104. }
  105. }
  106. }
  107. return coverchr;
  108. }
  109. }
  110. }