C# 中文簡體轉繁體實現代碼

來源:互聯網
上載者:User

方法一:

複製代碼 代碼如下:/// <summary>
/// 中文字元工具類
/// </summary>
private const int LOCALE_SYSTEM_DEFAULT = 0x0800;
private const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
private const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

[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);

/// <summary>
/// 將字元轉換成簡體中文
/// </summary>
/// <param name="source">輸入要轉換的字串</param>
/// <returns>轉換完成後的字串</returns>
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;
}

/// <summary>
/// 講字元轉換為繁體中文
/// </summary>
/// <param name="source">輸入要轉換的字串</param>
/// <returns>轉換完成後的字串</returns>
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;
}

調用上面的ToTraditiona方法就OK了~另外的那個一樣的使用方法~
方法二:(推薦)
①在解決方案管理器中對應的檔案夾右擊“添加引用”----選擇.net引用下的Microsoft.VisualBasic;
②在你要實現轉換功能的aspx.cs檔案中添加命名空間:using Microsoft.VisualBasic
③ 通過下面的方法可以直接實現轉換,很方便吧!一句話就可以了~所以推薦這個方法複製代碼 代碼如下: string s = "繁體";
s = Strings.StrConv(s, VbStrConv.Wide, 0); // 半形轉全形
s = Strings.StrConv(s, VbStrConv.TraditionalChinese, 0); // 簡體轉繁體
s = Strings.StrConv(s, VbStrConv.ProperCase , 0); // 首字母大寫
s = Strings.StrConv(s, VbStrConv.Narrow , 0); // 全形轉半形
s = Strings.StrConv(s, VbStrConv.SimplifiedChinese, 0); // 繁體轉簡體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.