简体字和繁体字转换四种方法

原文:简体字和繁体字转换四种方法

1.VisualBasic转换

   1>引用Microsoft.VisualBasic;

   2>Strings.StrConv(jian, VbStrConv.TraditionalChinese, 0);  //简体字转换为繁体字

        Strings.StrConv(jian, VbStrConv.SimplifiedChinese, 0); //繁体字转换为简体字

简体字和繁体字转换四种方法VisualBasic

2.ChineseConverter转换

   1>引用Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter

    2>string temp_1 = ChineseConverter.Convert("理发加上发财,闹钟加上一见钟情,后来", ChineseConversionDirection.SimplifiedToTraditional);

         string temp_2 = ChineseConverter.Convert("理髮加上發财,鬧鐘加上一見鍾情,後來", ChineseConversionDirection.TraditionalToSimplified);

简体字和繁体字转换四种方法
string temp_1 = ChineseConverter.Convert("理发加上发财,闹钟加上一见钟情,后来", ChineseConversionDirection.SimplifiedToTraditional);
            string temp_2 = ChineseConverter.Convert("理髮加上發财,鬧鐘加上一見鍾情,後來", ChineseConversionDirection.TraditionalToSimplified);
ChineseConverter

3.kernel32.dll转换

   1>引用System.Runtime.InteropServices

    2>代码如下: 引用以下方法的语句:

string F2J = ToTraditional(fanF, LCMAP_SIMPLIFIED_CHINESE);   //转简体

string J2F = ToTraditional(fanF, LCMAP_TRADITIONAL_CHINESE);  //转繁体

简体字和繁体字转换四种方法
[DllImport("kernel32.dll", EntryPoint = "LCMapStringA")]
        public static extern int LCMapString(int Locale, int dwMapFlags, byte[] lpSrcStr, int cchSrc, byte[] lpDestStr, int cchDest);
        const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
        const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

public static string ToTraditional(string source,int type)
        {
            byte[] srcByte2 = Encoding.Default.GetBytes(source);
            byte[] desByte2 = new byte[srcByte2.Length];
            LCMapString(2052, type, srcByte2, -1, desByte2, srcByte2.Length);
            string des2 = Encoding.Default.GetString(desByte2);
            return null;
        }
kernel32.dll

前三种方法转换对语义不能进行分析,比如说是:头发和发财的发对应的繁体字分别为:“髮”,“發”,前三种识别不了,转换完都为“發”

4.利用using Microsoft.Office.Interop.Word;

   1>引用Microsoft.Office.Interop.Word; 和   System.Reflection;两个命名空间,具体调用语句如下:

var fanW = CHS2CHT(jian);
var jianW = CHT2CHS(fanF);

简体字和繁体字转换四种方法
/// <summary>
        /// 法4:简体转繁体
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string CHS2CHT(string src)
        {
            string des = "";
            _Application appWord = new Application();
            object template = Missing.Value;
            object newTemplate = Missing.Value;
            object docType = Missing.Value;
            object visible = true;
            Document doc = appWord.Documents.Add(ref template, ref newTemplate, ref docType, ref visible);
            appWord.Selection.TypeText(src);
            appWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC, true, true);
            appWord.ActiveDocument.Select();
            des = appWord.Selection.Text;
            object saveChange = 0;
            object originalFormat = Missing.Value;
            object routeDocument = Missing.Value;
            appWord.Quit(ref saveChange, ref originalFormat, ref routeDocument);
            doc = null;
            appWord = null;
            GC.Collect();//进程资源释放
            return des;
        }
 /// <summary>
        /// 法4:繁体转简体
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string CHT2CHS(string src)
        {
            string des = "";
            _Application appWord = new Microsoft.Office.Interop.Word.Application();
            object template = Missing.Value;
            object newTemplate = Missing.Value;
            object docType = Missing.Value;
            object visible = true;
            Document doc = appWord.Documents.Add(ref template, ref newTemplate, ref docType, ref visible);
            appWord.Selection.TypeText(src);
            appWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC, true, true);
            appWord.ActiveDocument.Select();
            des = appWord.Selection.Text;
            object saveChange = 0;
            object originalFormat = Missing.Value;
            object routeDocument = Missing.Value;
            appWord.Quit(ref saveChange, ref originalFormat, ref routeDocument);
            doc = null;
            appWord = null;
            GC.Collect();//进程资源释放
            return des;
        }
        
Word

整个项目路径(VS2013): 链接:http://pan.baidu.com/s/1qXEUVAs 密码:mltv

上一篇:(踏上有意义的旅程)数据探索--找出规律揭示奥秘


下一篇:报错:selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'nam