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.

69 lines
1.8 KiB

6 months ago
  1. using Microsoft.International.Converters.PinYinConverter;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Shentun.ProjectManager
  8. {
  9. public class LanguageConverter
  10. {
  11. #region 新版
  12. /// <summary>
  13. /// 汉字转首字母
  14. /// </summary>
  15. /// <param name="strChinese"></param>
  16. /// <returns></returns>
  17. public static string GetPYSimpleCode(string strChinese)
  18. {
  19. try
  20. {
  21. if (strChinese.Length != 0)
  22. {
  23. StringBuilder fullSpell = new StringBuilder();
  24. for (int i = 0; i < strChinese.Length; i++)
  25. {
  26. var chr = strChinese[i];
  27. fullSpell.Append(GetSpell(chr)[0]);
  28. }
  29. return fullSpell.ToString().ToUpper();
  30. }
  31. }
  32. catch (Exception e)
  33. {
  34. Console.WriteLine("首字母转化出错!" + e.Message);
  35. }
  36. return string.Empty;
  37. }
  38. private static string GetSpell(char chr)
  39. {
  40. var coverchr = NPinyin.Pinyin.GetPinyin(chr);
  41. bool isChineses = ChineseChar.IsValidChar(coverchr[0]);
  42. if (isChineses)
  43. {
  44. ChineseChar chineseChar = new ChineseChar(coverchr[0]);
  45. foreach (string value in chineseChar.Pinyins)
  46. {
  47. if (!string.IsNullOrEmpty(value))
  48. {
  49. return value.Remove(value.Length - 1, 1);
  50. }
  51. }
  52. }
  53. return coverchr;
  54. }
  55. #endregion
  56. }
  57. }