C# 实现繁体字和简体字之间的转换

今天收到一个需求,将一组简体的汉字转换成繁体的汉字,刚开始有点茫然,后来在网上搜了一下思路,结果很少有涉及,终于我在看了MSDN后找到了如何解决,可能这方面对一些高程来说很Easy,但是除了高程还有很大一部分的初中程并不知道,所以我写这个只是提醒和帮助一下大家。下面分享下:

1. 想要实现这个程序的目的,我们就要先确定怎么去实现,是否会用到一些其他的类库,总之一句话,我们要先确定需求,然后根据需求去分析。

2. 言归正传,首先我们要引用Microsoft.VisualBasic这个类库

3. 其次我们为了看着方便,新建一个aspx的页面,在页面中放置4个控件就可以了,一个textbox,一个textarea,两个button,textbox用于输入要转换的汉字,textarea用于显示转换后的数字,button用于控制转换,页面效果如图:

4. 最后呢就是在aspx.cs中实现了,代码如下:

View Code
 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. 到这里我们需要的功能就实现了,效果如下:

posted @ 2012-03-16 11:20  JasonNET  阅读(12745)  评论(1编辑  收藏  举报