zoukankan      html  css  js  c++  java
  • C# 汉字转拼音、繁体

     Nuget:ChineseConverter、Microsoft.PinYinConverter

      

    添加帮助类 MsPinYinHelper

    using Microsoft.International.Converters.PinYinConverter;
    using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CDLZIMC.Transit.Common
    {
        public class MsPinYinHelper
        {
            /// <summary>
            /// 汉字转全拼
            /// </summary>
            /// <param name="chinese">汉字</param>
            /// <returns></returns>
            public static string PinYin(string chinese)
            {
                string retValue = string.Empty;
    
                foreach (char chr in chinese)
                {
                    try
                    {
                        if (ChineseChar.IsValidChar(chr))
                        {
                            ChineseChar chineseChar = new ChineseChar(chr);
                            string t = chineseChar.Pinyins[0].ToString();
                            retValue += t.Substring(0, t.Length - 1);
                        }
                        else
                        {
                            retValue += chr.ToString();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("全拼转化出错!" + e.Message);
                        return string.Empty;
                    }
                }
    
                return retValue;
            }
    
            /// <summary>
            /// 汉字转首拼
            /// </summary>
            /// <param name="chinese">汉字</param>
            /// <returns></returns>
            public static string FirstPinYin(string chinese)
            {
                string retValue = string.Empty;
    
                foreach (char chr in chinese)
                {
                    try
                    {
                        if (ChineseChar.IsValidChar(chr))
                        {
                            ChineseChar chineseChar = new ChineseChar(chr);
                            string t = chineseChar.Pinyins[0].ToString();
                            retValue += t.Substring(0, 1);
                        }
                        else
                        {
                            retValue += chr.ToString();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("首字母转化出错!" + e.Message);
                        return string.Empty;
                    }
                }
    
                return retValue;
            }
    
            /// <summary>
            /// 判断汉字是否是多音字
            /// </summary>
            /// <param name="chr">单个汉字字符</param>
            /// <returns></returns>
            public static bool IsPolyphone(char chr)
            {
                if (ChineseChar.IsValidChar(chr))
                {
                    ChineseChar chineseChar = new ChineseChar(chr);
                    return chineseChar.IsPolyphone;
                }
                else
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 判断汉字是否是多音字
            /// </summary>
            /// <param name="chinese">字符串形式的汉字,如果是多个,只判断首字</param>
            /// <returns></returns>
            public static bool IsPolyphone(string chinese)
            {
                if (ChineseChar.IsValidChar(chinese[0]))
                {
                    ChineseChar chineseChar = new ChineseChar(chinese[0]);
                    return chineseChar.IsPolyphone;
                }
                else
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 取得汉字拼音个数
            /// </summary>
            /// <param name="chinese">汉字字符串,非汉字字符不算长度</param>
            /// <returns></returns>
            public static int PinYinCount(string chinese)
            {
                int retCount = 0;
    
                foreach (char chr in chinese)
                {
                    if (ChineseChar.IsValidChar(chr))
                    {
                        ChineseChar chineseChar = new ChineseChar(chr);
                        retCount += chineseChar.PinyinCount;
                    }
                }
    
                return retCount;
            }
    
            /// <summary>
            /// 取得汉字拼音个数
            /// </summary>
            /// <param name="chr">单个汉字字符,非汉字返回0</param>
            /// <returns></returns>
            public static int PinYinCount(char chr)
            {
                if (ChineseChar.IsValidChar(chr))
                {
                    ChineseChar chineseChar = new ChineseChar(chr);
                    return chineseChar.PinyinCount;
                }
                else
                {
                    return 0;
                }
            }
    
            /// <summary>
            /// 取得汉字笔画数
            /// </summary>
            /// <param name="chinese">汉字字符串,非汉字的字符不算笔画</param>
            /// <returns></returns>
            public static int StrokeNumber(string chinese)
            {
                int retCount = 0;
    
                foreach (char chr in chinese)
                {
                    if (ChineseChar.IsValidChar(chr))
                    {
                        ChineseChar chineseChar = new ChineseChar(chr);
                        retCount += chineseChar.StrokeNumber;
                    }
                }
    
                return retCount;
            }
    
            /// <summary>
            /// 取得汉字笔画数
            /// </summary>
            /// <param name="chr">单个汉字字符,非汉字返回0</param>
            /// <returns></returns>
            public static int StrokeNumber(char chr)
            {
                if (ChineseChar.IsValidChar(chr))
                {
                    ChineseChar chineseChar = new ChineseChar(chr);
                    return chineseChar.StrokeNumber;
                }
                else
                {
                    return 0;
                }
            }
    
            /// <summary>
            /// 简体转繁体
            /// </summary>
            /// <param name="simplified"></param>
            /// <returns></returns>
            public static string SimplifiedToTraditional(string simplified)
            {
                return ChineseConverter.Convert(simplified, ChineseConversionDirection.SimplifiedToTraditional);
            }
    
            /// <summary>
            /// 繁体转简体
            /// </summary>
            /// <param name="traditional"></param>
            /// <returns></returns>
            public static string TraditionalToSimplified(string traditional)
            {
                return ChineseConverter.Convert(traditional, ChineseConversionDirection.TraditionalToSimplified);
            }
        }
    }
  • 相关阅读:
    Android开源库
    银行卡的数字检測
    hdu4941 Magical Forest
    android之检測是否有网络
    在Oracle数据库中使用NFS,怎样调优?
    centos+nginx+php-fpm+php include fastcgi_params php页面能訪问但空白,被fastcgi_params与fastcgi.conf害慘了
    漫谈反射
    Android 四大组件学习之BroadcastReceiver二
    【LeetCode】two num 利用comparable接口 对对象进行排序
    扩展功能==继承?
  • 原文地址:https://www.cnblogs.com/Allofus/p/15131575.html
Copyright © 2011-2022 走看看