C#實(shí)現(xiàn)漢字轉(zhuǎn)漢語(yǔ)拼音的示例代碼
一、使用PinYinConverterCore獲取漢語(yǔ)拼音
最新在做一個(gè)搜索組件,需要使用漢語(yǔ)拼音的首字母查詢出符合條件的物品名稱,由于漢字存在多音字,所以自己寫查詢組件不太現(xiàn)實(shí),因此,我們使用微軟提供的CHSPinYinConv,CHSPinYinConv在.net core下載安裝沒有問題,但在.net framework會(huì)由于兼容性會(huì)安裝失敗,因此使用了PinYinConverterCore來實(shí)現(xiàn)漢字轉(zhuǎn)拼音,PinYinConverterCore應(yīng)該也是基于CHSPinYinConv開發(fā)的兼容包,后續(xù)的代碼兩個(gè)安裝包環(huán)境下都可以使用。使用Nuget搜索PinYinConverterCore下載并安裝,具體如下:

二、編寫工具擴(kuò)展類實(shí)現(xiàn)獲取漢字的拼音
由于漢字存在多音字,因此,通過漢字獲取到的拼音是一個(gè)數(shù)組,具體如下:
/// <summary>
/// 漢字轉(zhuǎn)換拼音
/// </summary>
public static class PingYinUtil
{
private static Dictionary<int, List<string>> GetTotalPingYinDictionary(string text)
{
var chs = text.ToCharArray();
//記錄每個(gè)漢字的全拼
Dictionary<int, List<string>> totalPingYinList = new Dictionary<int, List<string>>();
for (int i = 0; i < chs.Length; i++)
{
var pinyinList = new List<string>();
//是否是有效的漢字
if (ChineseChar.IsValidChar(chs[i]))
{
ChineseChar cc = new ChineseChar(chs[i]);
pinyinList = cc.Pinyins.Where(p => !string.IsNullOrWhiteSpace(p)).ToList();
}
else
{
pinyinList.Add(chs[i].ToString());
}
//去除聲調(diào),轉(zhuǎn)小寫
pinyinList = pinyinList.ConvertAll(p => Regex.Replace(p, @"\d", "").ToLower());
//去重
pinyinList = pinyinList.Where(p => !string.IsNullOrWhiteSpace(p)).Distinct().ToList();
if (pinyinList.Any())
{
totalPingYinList[i] = pinyinList;
}
}
return totalPingYinList;
}
/// <summary>
/// 獲取漢語(yǔ)拼音全拼
/// </summary>
/// <param name="text">The string.</param>
/// <returns></returns>
public static List<string> GetTotalPingYin(this string text)
{
var result = new List<string>();
foreach (var pys in GetTotalPingYinDictionary(text))
{
var items = pys.Value;
if (result.Count <= 0)
{
result = items;
}
else
{
//全拼循環(huán)匹配
var newTotalPingYinList = new List<string>();
foreach (var totalPingYin in result)
{
newTotalPingYinList.AddRange(items.Select(item => totalPingYin + item));
}
newTotalPingYinList = newTotalPingYinList.Distinct().ToList();
result = newTotalPingYinList;
}
}
return result;
}
/// <summary>
/// 獲取漢語(yǔ)拼音首字母
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static List<string> GetFirstPingYin(this string text)
{
var result = new List<string>();
foreach (var pys in GetTotalPingYinDictionary(text))
{
var items = pys.Value;
if (result.Count <= 0)
{
result = items.ConvertAll(p => p.Substring(0, 1)).Distinct().ToList();
}
else
{
//首字母循環(huán)匹配
var newFirstPingYinList = new List<string>();
foreach (var firstPingYin in result)
{
newFirstPingYinList.AddRange(items.Select(item => firstPingYin + item.Substring(0, 1)));
}
newFirstPingYinList = newFirstPingYinList.Distinct().ToList();
result = newFirstPingYinList;
}
}
return result;
}
}三、編寫測(cè)試用例
我們編寫一個(gè)測(cè)試用例,通過輸入的漢字獲取到漢語(yǔ)拼音的全拼和首字母縮寫,具體如下:
// 漢字輸入
string text = TextBoxInput.Text;
// 獲取到漢語(yǔ)拼音的全拼
TextBoxTotal.Text = string.Join(",", text.GetTotalPingYin());
// 獲取到漢語(yǔ)拼音的首字母
TextBoxFirst.Text = string.Join(",", text.GetFirstPingYin());
我們編寫錄入一組用戶名,然后根據(jù)輸入輸入的用戶名的縮寫,篩選出符合條件的人,我們可以使用Linq模糊查詢,具體如下:
public class Student
{
public string Name { get; set; }
public List<string> Pinyin { get; set; }
} StudentList = new List<Student>
{
new Student() {Name = "張三"},
new Student() {Name = "章黎"},
new Student() {Name = "張三豐"},
new Student() {Name = "李四"},
new Student() {Name = "王五"},
new Student() {Name = "John"},
new Student() {Name = "W.吳"},
new Student() {Name = "阿姨"},
new Student() {Name = "阿膠"},
new Student() {Name = "麥合蘇提.麥合蘇提"}
};var text = TextBoxSearch.Text;
foreach (var student in StudentList)
{
student.Pinyin = student.Name.GetFirstPingYin();
}
StudentList = StudentList.Where(s => s.Pinyin.Exists(p=>p.Contains(text))).ToList();
到此這篇關(guān)于C#實(shí)現(xiàn)漢字轉(zhuǎn)漢語(yǔ)拼音的示例代碼的文章就介紹到這了,更多相關(guān)C#漢字轉(zhuǎn)拼音內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#自定義繁體和簡(jiǎn)體字庫(kù)實(shí)現(xiàn)中文繁體和簡(jiǎn)體之間轉(zhuǎn)換的方法
這篇文章主要介紹了C#自定義繁體和簡(jiǎn)體字庫(kù)實(shí)現(xiàn)中文繁體和簡(jiǎn)體之間轉(zhuǎn)換的方法,通過自定義繁簡(jiǎn)轉(zhuǎn)換字庫(kù)實(shí)現(xiàn)繁體與簡(jiǎn)體轉(zhuǎn)換的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#調(diào)用C++的dll兩種實(shí)現(xiàn)方式(托管與非托管)
這篇文章主要介紹了C#調(diào)用C++的dll兩種實(shí)現(xiàn)方式(托管與非托管),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
C#匿名委托和Java匿名局部?jī)?nèi)部類使用方法示例
Java在嵌套類型這里提供的特性比較多,假設(shè):Java的字節(jié)碼只支持靜態(tài)嵌套類,內(nèi)部類、局部?jī)?nèi)部類和匿名局部?jī)?nèi)部類都是編譯器提供的語(yǔ)法糖,這個(gè)假設(shè)目前沒法驗(yàn)證(看不懂字節(jié)碼),本文先來看一下C#是如何為我們提供的這種語(yǔ)法糖2013-11-11
Winform應(yīng)用程序如何使用自定義的鼠標(biāo)圖片
這篇文章主要介紹了Winform應(yīng)用程序如何使用自定義的鼠標(biāo)圖片,在window系統(tǒng)中,自帶的鼠標(biāo)外觀可能看起來比較小,因此我們需要使用自己的鼠標(biāo)圖片外觀2020-11-11
適用于WebForm Mvc的Pager分頁(yè)組件C#實(shí)現(xiàn)
這篇文章主要為大家分享了適用于WebForm Mvc的Pager分頁(yè)組件,由C#實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-04-04

