# Pinyin4Net **Repository Path**: SYXM/Pinyin4Net ## Basic Information - **Project Name**: Pinyin4Net - **Description**: .net使用的汉字转拼音库(含姓名拼音独立接口)。提供了简单的调用接口和通过拼音查询汉字的接口。 - **Primary Language**: C# - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 127 - **Created**: 2018-02-15 - **Last Updated**: 2020-12-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Pinyin4NET .net环境下使用的拼音-汉字互转库。 ## 接口 > 提供的所有接口,均为静态接口。 ### Pinyin4Net 汉字拼音查询 > 汉字查询接口都放在类 **Pinyin4Net** 内 #### 汉字查拼音 ```csharp /// /// 获取汉字的拼音数组 /// /// 要查询拼音的汉字字符 /// 汉字的拼音数组,若未找到汉字拼音,则返回空数组 /// 当要获取拼音的字符不是汉字时抛出此异常 public static string[] GetPinyin(char hanzi) /// /// 获取唯一拼音(单音字)或者第一个拼音(多音字) /// /// 要查询拼音的汉字字符 /// 返回唯一拼音(单音字)或者第一个拼音(多音字) /// 当要获取拼音的字符不是汉字时抛出此异常 public static string GetUniqueOrFirstPinyin(char hanzi) /// /// 获取格式化后的拼音 /// /// 要查询拼音的汉字字符 /// 拼音输出格式化参数 /// /// /// 返回经过格式化的拼音 /// 当要获取拼音的字符不是汉字时抛出此异常 public static string[] GetPinyinWithFormat(char hanzi, PinyinOutputFormat format) /// /// 获取格式化后的唯一拼音(单音字)或者第一个拼音(多音字) /// /// 要查询拼音的汉字字符 /// 拼音输出格式化参数 /// /// /// 格式化后的唯一拼音(单音字)或者第一个拼音(多音字) /// 当要获取拼音的字符不是汉字时抛出此异常 public static string GetUniqueOrFirstPinyinWithFormat(char hanzi, PinyinOutputFormat format) /// /// 获取一个字符串内所有汉字的拼音(多音字取第一个读音,带格式) /// /// 要获取拼音的汉字字符串 /// 拼音输出格式化参数 /// 是否将前面的格式中的大小写扩展到其它非拼音字符,默认为false。firstLetterOnly为false时有效 /// 是否只取拼音首字母,为true时,format无效 /// firstLetterOnly为true时有效,多音字的多个读音首字母是否全取,如果多音字拼音首字母相同,只保留一个 /// firstLetterOnly为true时,只取拼音首字母格式为[L],后面追加空格;multiFirstLetter为true时,多音字的多个拼音首字母格式为[L, H],后面追加空格 public static string GetPinyin(string text, PinyinOutputFormat format, bool caseSpread, bool firstLetterOnly, bool multiFirstLetter) /// /// 获取一个字符串内所有汉字的拼音(多音字取第一个读音,带格式) /// /// 要获取拼音的汉字字符串 /// 拼音输出格式化参数 /// 是否将前面的格式中的大小写扩展到其它非拼音字符,默认为false。firstLetterOnly为false时有效 /// /// 拼音处理器,在获取到拼音后通过这个来处理, /// 如果传null,则默认取第一个拼音(多音字), /// 参数: /// 1 string[] 拼音数组 /// 2 char 当前的汉字 /// 3 string 要转成拼音的字符串 /// return 拼音字符串,这个返回值将作为这个汉字的拼音放到结果中 /// /// firstLetterOnly为true时,只取拼音首字母格式为[L],后面追加空格;multiFirstLetter为true时,多音字的多个拼音首字母格式为[L, H],后面追加空格 public static string GetPinyin(string text, PinyinOutputFormat format, bool caseSpread, Func pinyinHandler) /// /// 获取一个字符串内所有汉字的拼音(多音字取第一个读音,带格式),format中指定的大小写模式不会扩展到非拼音字符 /// /// 要获取拼音的汉字字符串 /// 拼音输出格式化参数 /// 格式化后的拼音字符串 public static string GetPinyin(string text, PinyinOutputFormat format) ``` #### 拼音查汉字 ```csharp /// /// 根据单个拼音查询匹配的汉字 /// /// 要查询汉字的单个拼音 /// 是否全部匹配,为true时,匹配整个拼音,否则匹配开头字符 /// public static string[] GetHanzi(string pinyin, bool matchAll) ``` ### Pinyin4Name 姓名拼音查询 > 姓名查询接口都放在类 **Pinyin4Name** 内 ```csharp /// /// 获取姓的拼音,如果是复姓则由空格分隔 /// /// 要查询拼音的姓 /// 返回姓的拼音,若未找到姓,则返回null /// 当要获取拼音的字符不是汉字时抛出此异常 public static string GetPinyin(string firstName) /// /// 获取姓的首字母,如果是复姓则由空格分隔首字母 /// /// 要查询拼音的姓 /// 返回姓的拼音首字母,若未找到姓,则返回null /// 当要获取拼音的字符不是汉字时抛出此异常 public static string GetFirstLetter(string firstName) /// /// 获取格式化后的拼音 /// /// 要查询拼音的姓 /// 输出拼音格式化参数 /// /// /// 返回格式化后的拼音,若未找到姓,则返回null /// 当要获取拼音的字符不是汉字时抛出此异常 public static string GetPinyinWithFormat(string firstName, PinyinOutputFormat format) /// /// 根据拼音查询匹配的姓 /// /// /// 是否全部匹配,为true时,匹配整个拼音,否则匹配开头字符,此参数用于告知传入的拼音是完整拼音还是仅仅是声母 /// 匹配的姓数组 public static string[] GetHanzi(string pinyin, bool matchAll) ``` ### 格式化参数 > 用于对拼音输入进行格式化控制 ```csharp /// /// 大小写格式 /// public enum CaseFormat { /// /// 首字母大写,此选项对 a e o 几个独音无效 /// CAPITALIZE_FIRST_LETTER, /// /// 全小写 /// LOWERCASE, /// /// 全大写 /// UPPERCASE } /// /// 声调格式 /// public enum ToneFormat { /// /// 带声调标志 /// WITH_TONE_MARK, /// /// 不带声调 /// WITHOUT_TONE, /// /// 带声调数字值 /// WITH_TONE_NUMBER } /// /// V(ü)字符格式 /// public enum VCharFormat { /// /// 将 ü 输出为 u: /// WITH_U_AND_COLON, /// /// 将 ü 输出为 v /// WITH_V, /// /// 将 ü 输出为ü /// WITH_U_UNICODE } ``` 通过实例化输入格式化类 `PinyinOutputFormat`, 设置上面的这些枚举,再将其传给获取拼音的函数,如:`GetPinyinWithFormat` 等, 即可格式化拼音输入。(请看 [示例](#示例)) ## 示例 ```csharp // 设置拼音输出格式 PinyinOutputFormat format = new PinyinOutputFormat(ToneFormat.WITHOUT_TONE, CaseFormat.LOWERCASE, VCharFormat.WITH_U_UNICODE); char hanzi = '李'; // 判断是否是汉字 if(PinyinUtil.IsHanzi(hanzi)){ return; } // 取出指定汉字的所有拼音 string[] py = Pinyin4Net.GetPinyin(hanzi); // 取出指定汉字的所有拼音(经过格式化的) string[] py = Pinyin4Net.GetPinyin(hanzi, format); // 取指定汉字的唯一或者第一个拼音 Pinyin4Net.GetUniqueOrFirstPinyin(hanzi); // 取指定汉字的唯一或者第一个拼音(经过格式化的) Pinyin4Net.GetUniqueOrFirstPinyinWithFormat(hanzi, format); // 根据拼音查汉字 string[] hanzi = Pinyin4Net.GetHanzi('li', true); ``` ### 姓名拼音查询 ```csharp string firstName = "单于"; // 取出姓的拼音 string py = Pinyin4Name.GetPinyin(firstName); // 取出姓的拼音首字母 string py = Pinyin4Name.GetFirstLetter(firstName); // 取出姓的拼音(格式化后) string py = Pinyin4Name.GetPinyinWithFormat(firstName, format); // 取出匹配拼音的姓 string[] firstNames = Pinyin4Name.GetHanzi("li", false); ```