zoukankan      html  css  js  c++  java
  • C# 实现繁体字和简体字之间的转换

    今天收到一个需求,将一组简体的汉字转换成繁体的汉字,刚开始有点茫然,后来在网上搜了一下思路,结果很少有涉及,终于我在看了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. 到这里我们需要的功能就实现了,效果如下:

     
     
     
    好文要顶 关注我 收藏该文  
  • 相关阅读:
    【独家】K8S漏洞报告 | 近期bug fix解读
    idou老师教你学Istio 29:Envoy启动流程
    idou老师教你学Istio 28:istio-proxy check 的缓存
    idou老师教你学Istio :5分钟简析Istio异常检测
    idou老师教你学Istio 27:解读Mixer Report流程
    idou老师教你学Istio 26:如何使用Grafana进行可视化监控
    idou老师教你学Istio 25:如何用istio实现监控和日志采集
    idou老师教你学Istio 24:如何在Istio使用Prometheus进行监控
    idou老师教你学Istio 23 : 如何用 Istio 实现速率限制
    idou老师教你学Istio 22 : 如何用istio实现调用链跟踪
  • 原文地址:https://www.cnblogs.com/flish/p/5056827.html
Copyright © 2011-2022 走看看