當前位置:首頁>知識>c的漢語和諧音(C漢字與拼音互轉)
發布時間:2024-01-23閱讀(9)
這個功能,大家也都可以去百度以下,千篇一律都自己寫的(抄的)封裝好的公共類,此處還是得膜拜下原創的大佬,可以花時間去搞這個,我看著都頭皮發麻。
對于一個有代碼潔癖的碼農來說,我并不喜歡那種方式(自己抄下來封裝?那我圖個啥?),所以我們直接從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); }
實現效果:

簡單來說,這個功能其實也是相當于一本漢語詞典的功能。
當然了,上面說的開源的公共類,最起碼擴展性不錯,遇到沒有的文字的時候可以自定義新增。
由簡入繁,拿來即用
后續精彩,持續關注
下一篇:紅娘是哪一部作品中的人物
Copyright ? 2024 有趣生活 All Rights Reserve吉ICP備19000289號-5 TXT地圖HTML地圖XML地圖