C# 汉字转拼音
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
一、简述微软官方提供了一个类库包
二、安装使用1)打开下载地址,解压安装 CHSPinYinConv.msi 2)打开默认安装目录:C:\Program Files (x86)\Microsoft Visual Studio International Pack\Simplified Chinese Pin-Yin Conversion Library\ 3)在项目中,添加引用,ChnCharInfo.dll 三、源码 1 public static class PinYinHelper
2 {
3 /// <summary>
4 /// 返回字符串的简拼
5 /// </summary>
6 /// <param name="inputTxt"></param>
7 /// <returns></returns>
8 public static string GetShortPinYin(string inputTxt)
9 {
10 string shortPinYin = "";
11 foreach (char c in inputTxt.Trim())
12 {
13 if (ChineseChar.IsValidChar(c))
14 {
15 ChineseChar chineseChar = new ChineseChar(c);
16 shortPinYin += chineseChar.Pinyins[0].Substring(0, 1).ToUpper();
17 continue;
18 }
19
20 if (Regex.IsMatch(c.ToString(), @"^[a-zA-Z]$"))
21 {
22 shortPinYin += c.ToString().ToUpper();
23 continue;
24 }
25
26 shortPinYin += "?";
27 }
28 return shortPinYin;
29 }
30
31 /// <summary>
32 /// 返回字符串全拼
33 /// </summary>
34 /// <param name="inputTxt"></param>
35 /// <returns></returns>
36 public static string GetAllPinYin(string inputTxt)
37 {
38 string allPinYin = "";
39 foreach (char c in inputTxt.Trim())
40 {
41 ChineseChar chineseChar = new ChineseChar(c);
42 allPinYin += chineseChar.Pinyins[0].Substring(0, chineseChar.Pinyins[0].Length - 1).ToUpper();
43 }
44 return allPinYin;
45 }
46 }
该文章在 2021/1/30 10:35:17 编辑过 |
关键字查询
相关文章
正在查询... |