48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using Microsoft.International.Converters.PinYinConverter;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace SOH.Common
|
|
{
|
|
public static class ChnToPinYin
|
|
{
|
|
public static string ToPinyin(string hanzi)
|
|
{
|
|
char[] ch = hanzi.ToArray();
|
|
string pinyinStr = "";
|
|
foreach (char c in ch)
|
|
{
|
|
pinyinStr += ToPinyin(c);
|
|
}
|
|
return pinyinStr.ToLower();
|
|
}
|
|
public static string ToPinyin(char c)
|
|
{
|
|
if (ChineseChar.IsValidChar(c))
|
|
{
|
|
ChineseChar chineseChar = new ChineseChar(c);
|
|
ReadOnlyCollection<string> pinyin = chineseChar.Pinyins;
|
|
return (pinyin[0].Substring(0, pinyin[0].Length - 1));
|
|
}
|
|
else
|
|
{
|
|
return c.ToString();
|
|
}
|
|
}
|
|
|
|
public static string HeadFirst(string hanzi)
|
|
{
|
|
char[] ch = hanzi.ToArray();
|
|
string pinyinStr = "";
|
|
foreach (char c in ch)
|
|
{
|
|
pinyinStr += ToPinyin(c).Substring(0,1);
|
|
}
|
|
return pinyinStr.ToLower();
|
|
}
|
|
}
|
|
}
|