{转}C#使用微软语言包来获取中文汉字的拼音、拼音首字母、中文繁体

首先需要获取ChnCharInfo.dll和ChineseConverter.dll


ChnCharInfo.dll官方下载操作如下:

先下载微软Microsoft Visual Studio International Pack 1.0 SR1语言包:

Microsoft Visual Studio International Pack 1.0 SR1

Microsoft Visual Studio International Feature Pack 2.0


下载后分别是“vsintlpack1.zip”、“Vsintlpack2.msi”、双击“Vsintlpack2.msi”安装完成之后解压“vsintlpack1.zip”,获得七个语言包。双击其中的“CHSPinYinConv.msi”完成安装后就可以在Visual Studio中引用了。

默认的语言包类库文件引用地址:“C:\Program Files (x86)\Microsoft Visual Studio International Pack\Simplified Chinese Pin-Yin Conversion Library\ChnCharInfo.dll”



示例:

using引用命名空间:

using Microsoft.International.Converters.PinYinConverter;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.International.Converters.PinYinConverter;
  7. using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
  8. namespace ConsoleApplication1
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Console.WriteLine( "汉字转全拼结果:" + GetPinyin( "测试"));
  15. Console.WriteLine( "汉字转全拼首字母结果:" + GetFirstPinyin( "测试"));
  16. Console.WriteLine( "简体转繁体结果:" + GetTraditionalChinese( "测试"));
  17. Console.WriteLine( "繁体转简体结果:" + GetSimplifiedChinese( "測試"));
  18. Console.ReadLine();
  19. }
  20. /// <summary>
  21. /// 汉字转化为拼音
  22. /// </summary>
  23. /// <param name="str">汉字</param>
  24. /// <returns>全拼</returns>
  25. public static string GetPinyin(string str)
  26. {
  27. string r = string.Empty;
  28. foreach ( char obj in str)
  29. {
  30. try
  31. {
  32. ChineseChar chineseChar = new ChineseChar(obj);
  33. string t = chineseChar.Pinyins[ 0].ToString();
  34. r += t.Substring( 0, t.Length - 1);
  35. }
  36. catch
  37. {
  38. r += obj.ToString();
  39. }
  40. }
  41. return r;
  42. }
  43. /// <summary>
  44. /// 汉字转化为拼音首字母
  45. /// </summary>
  46. /// <param name="str">汉字</param>
  47. /// <returns>首字母</returns>
  48. public static string GetFirstPinyin(string str)
  49. {
  50. string r = string.Empty;
  51. foreach ( char obj in str)
  52. {
  53. try
  54. {
  55. ChineseChar chineseChar = new ChineseChar(obj);
  56. string t = chineseChar.Pinyins[ 0].ToString();
  57. r += t.Substring( 0, 1);
  58. }
  59. catch
  60. {
  61. r += obj.ToString();
  62. }
  63. }
  64. return r;
  65. }
  66. /// <summary>
  67. /// 简体中文转化为繁体中文
  68. /// </summary>
  69. /// <param name="str">汉字</param>
  70. /// <returns>繁体中文</returns>
  71. public static string GetTraditionalChinese(string str)
  72. {
  73. if ( string.IsNullOrWhiteSpace(str.Replace( " ", "")))
  74. {
  75. return "";
  76. }
  77. string result = null;
  78. char[] CharSimples = str.ToCharArray();
  79. for ( int i = 0; i < CharSimples.Length; i++)
  80. {
  81. if (ChineseChar.IsValidChar(CharSimples[i]))
  82. {
  83. result += ChineseConverter.Convert(CharSimples[i].ToString(),
  84. ChineseConversionDirection.SimplifiedToTraditional);
  85. }
  86. else
  87. {
  88. result += CharSimples[i].ToString();
  89. }
  90. }
  91. return result;
  92. }
  93. /// <summary>
  94. /// 繁体中文转化为简体中文
  95. /// </summary>
  96. /// <param name="str">汉字</param>
  97. /// <returns>简体中文</returns>
  98. public static string GetSimplifiedChinese(string str)
  99. {
  100. if ( string.IsNullOrWhiteSpace(str.Replace( " ", "")))
  101. {
  102. return "";
  103. }
  104. string result = null;
  105. char[] CharSimples = str.ToCharArray();
  106. for ( int i = 0; i < CharSimples.Length; i++)
  107. {
  108. if (ChineseChar.IsValidChar(CharSimples[i]))
  109. {
  110. result += ChineseConverter.Convert(CharSimples[i].ToString(),
  111. ChineseConversionDirection.TraditionalToSimplified);
  112. }
  113. else
  114. {
  115. result += CharSimples[i].ToString();
  116. }
  117. }
  118. return result;
  119. }
  120. }
  121. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1)支持获取简体中文字符的常用属性:拼音,多音字,同音字,笔画 2)繁简中文互相化 public string Convert (string chr) { try{ if (chr.Length != 0) { StringBuilder fullSpell = new StringBuilder (); for (int i=0; i<chr.Length; i++) { bool isChineses = Microsoft.International.Converters.PinYinConverter.ChineseChar.IsValidChar(chr[i]); if (isChineses) { Microsoft.International.Converters.PinYinConverter.ChineseChar chineseChar = new Microsoft.International.Converters.PinYinConverter.ChineseChar (chr [i]); foreach (string value in chineseChar.Pinyins) { if (!string.IsNullOrEmpty (value)) { fullSpell.Append (value.Remove(value.Length - 1, 1)); break; } } }else { fullSpell.Append(chr[i]); } } return fullSpell.ToString().ToUpper(); } }catch(Exception e){ Console.WriteLine("全拼化出错!"+e.Message); } return string.Empty; } /// <summary> /// 获取首字母 /// </summary> public string SubFirstLetter(string chr) { try{ chr = chr.Substring(0,1); if (chr.Length != 0) { StringBuilder fullSpell = new StringBuilder (); for (int i=0; i<chr.Length; i++) { bool isChineses = Microsoft.International.Converters.PinYinConverter.ChineseChar.IsValidChar(chr[i]); if (isChineses) { Microsoft.International.Converters.PinYinConverter.ChineseChar chineseChar = new Microsoft.International.Converters.PinYinConverter.ChineseChar(chr[i]); foreach (string value in chineseChar.Pinyins) { if (!string.IsNullOrEmpty (value)) { fullSpell.Append (value.Remove(value.Length - 1, 1)); break; } } }else { fullSpell.Append(chr[i]); } } return fullSpell.ToString().Substring(0,1).ToUpper(); } }catch(Exception e){ //Log.Error("首字母化出错!"+e.Message); } return string.Empty; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值