123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using Microsoft.International.Converters.PinYinConverter;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using NUnit.Framework.Internal;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace TEAMModelOS.SDK
- {
- public static class PingYinHelper
- {
- /// <summary>
- /// 汉字转全拼
- /// </summary>
- /// <param name="strChinese"></param>
- /// <returns></returns>
- public static string ConvertToAllSpell(string strChinese)
- {
- try
- {
- if (strChinese.Length != 0)
- {
- StringBuilder fullSpell = new StringBuilder();
- for (int i = 0; i < strChinese.Length; i++)
- {
- var chr = strChinese[i];
- fullSpell.Append(GetSpell(chr));
- }
- return fullSpell.ToString().ToLower();
- }
- }
- catch (Exception e)
- {
- }
- return string.Empty;
- }
- /// <summary>
- /// 汉字转首字母
- /// </summary>
- /// <param name="strChinese"></param>
- /// <returns></returns>
- public static string GetFirstSpell(string strChinese)
- {
- //NPinyin.Pinyin.GetInitials(strChinese) 有Bug 洺无法识别
- //return NPinyin.Pinyin.GetInitials(strChinese);
- try
- {
- if (strChinese.Length != 0)
- {
- StringBuilder fullSpell = new StringBuilder();
- for (int i = 0; i < strChinese.Length; i++)
- {
- var chr = strChinese[i];
- fullSpell.Append(GetSpell(chr)[0]);
- }
- return fullSpell.ToString().ToLower();
- }
- }
- catch (Exception e)
- {
- }
- return string.Empty;
- }
- public static string GetSecondSpell(string strChinese)
- {
- //NPinyin.Pinyin.GetInitials(strChinese) 有Bug 洺无法识别
- //return NPinyin.Pinyin.GetInitials(strChinese);
- try
- {
- if (strChinese.Length != 0)
- {
- StringBuilder fullSpell = new StringBuilder();
- for (int i = 0; i < strChinese.Length; i++)
- {
- var chr = strChinese[i];
- var a = GetSpell(chr);
- if (a.Length > 1)
- {
- fullSpell.Append(a[1]);
- }
- else {
- fullSpell.Append(a[0]);
- }
- }
- return fullSpell.ToString().ToLower();
- }
- }
- catch (Exception e)
- {
- }
- return string.Empty;
- }
- public static string GetSpell(char chr)
- {
- var coverchr = NPinyin.Pinyin.GetPinyin(chr);
- bool isChineses = ChineseChar.IsValidChar(coverchr[0]);
- if (isChineses)
- {
- ChineseChar chineseChar = new ChineseChar(coverchr[0]);
- foreach (string value in chineseChar.Pinyins)
- {
- if (!string.IsNullOrEmpty(value))
- {
- return value.Remove(value.Length - 1, 1);
- }
- }
- }
- return coverchr;
- }
- }
- }
|