今天收到一個需求,將一組簡體的漢字轉換成繁體的漢字,剛開始有點茫然,后來在網上搜了一下思路,結果很少有涉及,終於我在看了MSDN后找到了如何解決,可能這方面對一些高程來說很Easy,但是除了高程還有很大一部分的初中程並不知道,所以我寫這個只是提醒和幫助一下大家。下面分享下:
1. 想要實現這個程序的目的,我們就要先確定怎么去實現,是否會用到一些其他的類庫,總之一句話,我們要先確定需求,然后根據需求去分析。
2. 言歸正傳,首先我們要引用Microsoft.VisualBasic這個類庫
3. 其次我們為了看着方便,新建一個aspx的頁面,在頁面中放置4個控件就可以了,一個textbox,一個textarea,兩個button,textbox用於輸入要轉換的漢字,textarea用於顯示轉換后的數字,button用於控制轉換,頁面效果如圖:
4. 最后呢就是在aspx.cs中實現了,代碼如下:

1 /// <summary>
2 /// 轉繁體
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 protected void Button1_Click(object sender, EventArgs e)
7 {
8 if (string.IsNullOrEmpty(txt_value.Text))
9 {
10 return;
11 }
12 else
13 {
14 string value = txt_value.Text.Trim();
15 string newValue = StringConvert(value, "1");
16 if (!string.IsNullOrEmpty(newValue))
17 {
18 TextArea1.Value = newValue;
19 }
20 }
21 }
22 /// <summary>
23 /// 轉簡體
24 /// </summary>
25 /// <param name="sender"></param>
26 /// <param name="e"></param>
27 protected void Button2_Click(object sender, EventArgs e)
28 {
29 if (string.IsNullOrEmpty(txt_value.Text))
30 {
31 return;
32 }
33 else
34 {
35 string value = txt_value.Text.Trim();
36 string newValue = StringConvert(value, "2");
37 if (!string.IsNullOrEmpty(newValue))
38 {
39 TextArea1.Value = newValue;
40 }
41 }
42 }
43
44 #region IString 成員
45
46 public string StringConvert(string x, string type)
47 {
48 String value = String.Empty;
49 switch (type)
50 {
51 case "1"://轉繁體
52 value = Microsoft.VisualBasic.Strings.StrConv(x, Microsoft.VisualBasic.VbStrConv.TraditionalChinese,0);
53 break;
54 case "2":
55 value = Microsoft.VisualBasic.Strings.StrConv(x, Microsoft.VisualBasic.VbStrConv.SimplifiedChinese, 0);
56 break;
57 default:
58 break;
59 }
60 return value;
61 }
62
63 #endregion
5. 到這里我們需要的功能就實現了,效果如下: