簡體字和繁體字轉換四種方法


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM