繁簡體轉換

using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
using System;
using System.Runtime.InteropServices;

namespace CaseName.Model
{     

    /// 
    /// 繁簡體轉換
    /// 
    public class charTorSchanged
    {
        [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);

        private const int LOCALE_SYSTEM_DEFAULT = 0x0800;
        private const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
        private const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;


        /// 
        /// 将字符转换成简体中文
        /// 
        /// 输入要转换的字符串
        /// 转换完成后的字符串
        public static string ToSimplified(string source)
        {
            String target = new String(' ', source.Length);
            int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, source, source.Length, target, source.Length);
            return target;
        }

        /// 
        /// 將字符转换为繁体中文
        /// 
        /// 输入要转换的字符串
        /// 转换完成后的字符串
        public static string ToTraditional(string source)
        {
            String target = new String(' ', source.Length);
            int ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, source, source.Length, target, source.Length);
            return target;
        }

        /// 
        /// 将字符转换成简体中文
        /// 
        /// 输入要转换的字符串
        /// 转换完成后的字符串
        public static string strToSimplified(string source)
        {
            return ChineseConverter.Convert(source, ChineseConversionDirection.TraditionalToSimplified);
        }

    }

}
///可以先將ChineseConverter寫出來,添加引用。

這是在網上收集整理的。

你可能感兴趣的:(C#)