本文實例講述了C#利用微軟自帶庫進行中文繁體和簡體之間轉換的方法。分享給大家供大家參考。具體分析如下:
下面的代碼是一個簡單的轉換范例,真正的核心轉換語句只有一句話,其它的都是界面和數據相關的,使用前需要引用Microsoft.VisualBasic這個類庫
/// <summary>/// 轉繁體/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void Button1_Click(object sender, EventArgs e){ if (string.IsNullOrEmpty(txt_value.Text)) { return; } else { string value = txt_value.Text.Trim(); string newValue = StringConvert(value, "1"); if (!string.IsNullOrEmpty(newValue)) { TextArea1.Value = newValue; } }}/// <summary>/// 轉簡體/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void Button2_Click(object sender, EventArgs e){ if (string.IsNullOrEmpty(txt_value.Text)) { return; } else { string value = txt_value.Text.Trim(); string newValue = StringConvert(value, "2"); if (!string.IsNullOrEmpty(newValue)) { TextArea1.Value = newValue; } }}#region IString 成員public string StringConvert(string x, string type){ String value = String.Empty; switch (type) { case "1"://轉繁體 value = Microsoft.VisualBasic.Strings.StrConv(x, Microsoft.VisualBasic.VbStrConv.TraditionalChinese,0); break; case "2": value = Microsoft.VisualBasic.Strings.StrConv(x, Microsoft.VisualBasic.VbStrConv.SimplifiedChinese, 0); break; default: break; } return value;}#endregion
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答