Пример #1
0
        /// <summary>
        /// 获取格式化后的唯一拼音(单音字)或者第一个拼音(多音字)
        /// </summary>
        /// <param name="hanzi">要查询拼音的汉字字符</param>
        /// <param name="format">拼音输出格式化参数</param>
        /// <see cref="PinyinFormat"/>
        /// <seealso cref="PinyinUtil"/>
        /// <returns>格式化后的唯一拼音(单音字)或者第一个拼音(多音字)</returns>
        /// <exception cref="UnsupportedUnicodeException">当要获取拼音的字符不是汉字时抛出此异常</exception>
        public static string GetFirstPinyin(char hanzi, PinyinFormat format = PinyinFormat.None)
        {
            var pinyin = GetPinyin(hanzi)[0];

            if (format == PinyinFormat.None)
            {
                return(pinyin);
            }
            return(PinyinUtil.Format(pinyin, format));
        }
Пример #2
0
        /// <summary>
        /// 获取汉字的拼音数组
        /// </summary>
        /// <param name="hanzi">要查询拼音的汉字字符</param>
        /// <param name="format">设置输出拼音的格式</param>
        /// <returns>汉字的拼音数组,若未找到汉字拼音,则返回空数组</returns>
        /// <exception cref="UnsupportedUnicodeException">当要获取拼音的字符不是汉字时抛出此异常</exception>
        public static string[] GetPinyin(char hanzi, PinyinFormat format = PinyinFormat.None)
        {
            if (!PinyinUtil.IsHanzi(hanzi))
            {
                // 不是汉字
                throw new UnsupportedUnicodeException("不支持的字符: 请输入汉字");
            }
            var pinyin = PinyinDB.Instance.GetPinyin(hanzi);

            if (format == PinyinFormat.None)
            {
                return(pinyin);
            }
            return(pinyin.Select(item => PinyinUtil.Format(item, format)).ToArray());
        }
Пример #3
0
        /// <summary>
        /// 获取姓的拼音,如果是复姓则由空格分隔
        /// </summary>
        /// <param name="firstName">要查询拼音的姓</param>
        /// <param name="format">输出拼音格式化参数</param>
        /// <returns>返回姓的拼音,若未找到姓,则返回null</returns>
        /// <exception cref="UnsupportedUnicodeException">当要获取拼音的字符不是汉字时抛出此异常</exception>
        public static string GetPinyin(string firstName, PinyinFormat format = PinyinFormat.None)
        {
            if (!firstName.All(PinyinUtil.IsHanzi))
            {
                // 不是汉字
                throw new UnsupportedUnicodeException("不支持的字符: 请输入汉字字符");
            }
            var pinyin = NameDB.Instance.GetPinyin(firstName);

            if (format == PinyinFormat.None)
            {
                return(pinyin);
            }

            return(string.Join(" ", pinyin.Split(' ').Select(item => PinyinUtil.Format(item, format))));
        }