C# 漢字轉(zhuǎn)拼音
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
一、簡(jiǎn)述微軟官方提供了一個(gè)類庫(kù)包
二、安裝使用1)打開(kāi)下載地址,解壓安裝 CHSPinYinConv.msi 2)打開(kāi)默認(rèn)安裝目錄:C:\Program Files (x86)\Microsoft Visual Studio International Pack\Simplified Chinese Pin-Yin Conversion Library\ 3)在項(xiàng)目中,添加引用,ChnCharInfo.dll 三、源碼 1 public static class PinYinHelper
2 {
3 /// <summary>
4 /// 返回字符串的簡(jiǎn)拼
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 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |