zoukankan      html  css  js  c++  java
  • .net 繁简转换

    public static class WordHelper
        {
            /// <summary>
            /// 将汉字转换成拼音
            /// </summary>
            /// <param name="str"></param>
            /// <param name="leftSeparate">间隔符(左)</param>
            /// <param name="rightSeparate">间隔符(右)</param>
            /// <param name="allPinYin">返回全部拼音</param>
            /// <param name="keepTone">保留音调</param>
            /// <returns></returns>
            public static string ToPinYin(string str, string leftSeparate, string rightSeparate, bool allPinYin, bool keepTone)
            {
                if (string.IsNullOrEmpty(str)) return "";
                leftSeparate = leftSeparate ?? "";
                rightSeparate = rightSeparate ?? "";
                StringBuilder result = new StringBuilder();
                for (int i = 0; i < str.Length; i++)
                {
                    try
                    {
                        ChineseChar cc = new ChineseChar(str[i]);
                        string py = "";
                        HashSet<string> used = new HashSet<string>();
                        for (int n = 0; n < (allPinYin ? cc.PinyinCount : 1); n++)
                        {
                            py = keepTone ? cc.Pinyins[n] : cc.Pinyins[n].Substring(0, cc.Pinyins[n].Length - 1);
                            if (!used.Contains(py))
                            {
                                result = result.Append(leftSeparate + py + rightSeparate);
                                used.Add(py);
                            }
                        }
                    }
                    catch
                    {
                        result = result.Append(str[i]);
                    }
                }
                return result.ToString();
            }
    
            public static string ToPinYin(string str, string leftSeparate, string rightSeparate)
            {
                return ToPinYin(str, leftSeparate, rightSeparate, false, false);
            }
    
            /// <summary>
            /// 将汉字转换成拼音
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string ToPinYin(string str)
            {
                return ToPinYin(str, "", "", false, false);
            }
    
            /// <summary>
            /// 是否是汉字
            /// </summary>
            /// <param name="c"></param>
            /// <returns></returns>
            public static bool IsChineseCharacter(char c)
            {
                try
                {
                    ChineseChar cc = new ChineseChar(c);
                    return true;
                }
                catch
                {
                }
                return false;
            }
    
            /// <summary>
            /// 获得笔画数
            /// </summary>
            /// <param name="c"></param>
            /// <returns></returns>
            public static short ToStrokeNumber(char c)
            {
                short result = 0;
                try
                {
                    ChineseChar cc = new ChineseChar(c);
                    result = cc.StrokeNumber;
                }
                catch
                {
                }
                return result;
            }
    
            /// <summary>
            /// 简体转繁体
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string ToTraditional(string str)
            {
                return ChineseConverter.Convert(str, ChineseConversionDirection.SimplifiedToTraditional);
                
            }
    
            /// <summary>
            /// 繁体转简体
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static string ToSimplified(string str)
            {
                return ChineseConverter.Convert(str, ChineseConversionDirection.TraditionalToSimplified);
            }
        }
    

      

    using Microsoft.International.Converters.PinYinConverter;
    using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
    

      

  • 相关阅读:
    Linux编译工具:gcc入门
    Socket编程实践(3) 多连接服务器实现与简单P2P聊天程序例程
    Socket编程实践(2) Socket API 与 简单例程
    Socket编程实践(1) 基本概念
    数据结构图文解析之:二分查找及与其相关的几个问题解析
    数据结构图文解析之:直接插入排序及其优化(二分插入排序)解析及C++实现
    [CG编程] 基本光照模型的实现与拓展以及常见光照模型解析
    郑重声明!本博客内容皆为原创且首发于博客园
    【原创】面试官:谈谈你对mysql联合索引的认识?
    【原创】为什么Mongodb索引用B树,而Mysql用B+树?
  • 原文地址:https://www.cnblogs.com/caolingyi/p/8548162.html
Copyright © 2011-2022 走看看