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

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

转载于:https://www.cnblogs.com/myyBlog/p/7724832.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值