這個功能,大家也都可以去百度以下,千篇一律都自己寫的(抄的)封裝好的公共類,此處還是得膜拜下原創的大佬,可以花時間去搞這個,我看着都頭皮發麻。
對于一個有代碼潔癖的碼農來說,我并不喜歡那種方式(自己抄下來封裝?那我圖個啥?),所以我們直接從NuGet去獲取一個封裝好的DLL:NPinyin
此DLL提供了三個方法(重載不算)
實現功能:
輸入漢字獲取拼音或者拼音的首字母,輸入拼音獲取對應的漢字
開發環境:
開發工具: Visual Studio 2013
.NET Framework版本:4.5
實現代碼:
/// <summary>
/// 漢字轉拼音
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPinyin_Click(object sender, EventArgs e)
{
txtPinyin.Text = NPinyin.Pinyin.GetPinyin(txtChinese.Text);
}
/// <summary>
/// 拼音轉漢字(單字)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnChinese_Click(object sender, EventArgs e)
{
txtChinese.Text = NPinyin.Pinyin.GetChineseText(txtPinyin.Text);
}
/// <summary>
/// 獲取漢字首字母
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFirst_Click(object sender, EventArgs e)
{
txtPinyin.Text = NPinyin.Pinyin.GetInitials(txtChinese.Text);
}
實現效果:
簡單來說,這個功能其實也是相當于一本漢語詞典的功能。
當然了,上面說的開源的公共類,最起碼擴展性不錯,遇到沒有的文字的時候可以自定義新增。
由簡入繁,拿來即用
後續精彩,持續關注
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!