當我們遇到要處理漢字和拼音之間的轉化關系怎麼辦?如和用程式來實作?
我搜尋到一個ChineseChar開發包,然後實作了這一難題
using System;
using Microsoft.International.Converters.PinYinConverter;
namespace 拼音基礎
{
class Program
{
static void Main(string[] args)
{
#region 判斷是否為同音字
ChineseChar chineseChar = new ChineseChar('微');
Console.WriteLine("Stroke number of 微 in Chinese is {0}.", chineseChar.StrokeNumber);
Console.WriteLine("{0} characters' pinyin is \"wei1\".", ChineseChar.GetHomophoneCount("wei1"));
if (ChineseChar.IsHomophone('微', '薇'))
{
Console.WriteLine("微 and 薇 have the same pinyin.");
}
else
Console.WriteLine("微 and 薇 have different pinyins.");
}
#endregion
ChineseChar char1 = new ChineseChar('單');
bool f = ChineseChar.IsHomophone('楊','洋');
Console.Write("楊和洋是否為同音字"+f);
Console.Write("\n單是否為多音字:"+char1.IsPolyphone);
char[] chars = ChineseChar.GetChars("ji3");//要加上聲調
foreach (char c in chars)
Console.Write(c + " ");
for (int i = 0; i < char1.PinyinCount; i++)
string s=char1.Pinyins[i];
Console.WriteLine(s);
//判斷是否是一個拼音字元串
Console.WriteLine("de是否是一個合法的拼音"+ChineseChar.IsValidPinyin("de1"));//1,2,3,4表示聲調
#region 輸入一段中文,寫出拼音
string str = Console.ReadLine();
foreach (char c in str)
if (ChineseChar.IsValidChar(c))
{
ChineseChar cc = new ChineseChar(c);
Console.Write(cc.Pinyins[0] + " ");
}
else
Console.Write(c);
Console.Read();
}
}
}
本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366791,如需轉載請自行聯系原作者