C# 實現繁體字和簡體字之間的轉換

來源:互聯網
上載者:User

今天收到一個需求,將一組簡體的漢字轉換成繁體的漢字,剛開始有點茫然,後來在網上搜了一下思路,結果很少有涉及,終於我在看了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. 到這裡我們需要的功能就實現了,效果如下:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.