[C#]使用 Microsoft.VisualBasic VbStrConv 進行全型轉半型、簡體轉繁體

  • 10514
  • 0
  • .NET
  • 2010-11-08

[C#]使用 Microsoft.VisualBasic VbStrConv 進行全型轉半型、簡體轉繁體

 

在進行字串處理的時候,若需要使用「全型轉半型」或簡單版本的「簡體轉繁體」,
可以使用 Microsoft.VisualBasic  VbStrConv 方法,快速的進行轉換。
使用的方式如下:

 

  1. 加入參考 Microsoft.VisualBasic
  2. .cs 頁面增加 using Microsoft.VisualBasic;
  3. 以下為測試頁面 .aspx  .cs

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NarrowtToWide.aspx.cs" Inherits="NarrowtToWide" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtInput" runat="server">abcde這是使用VbStrConv的測試12345</asp:TextBox> 
        <asp:Button ID="btnConvert" runat="server" Text="轉換" 
            onclick="btnConvert_Click" />
        <p></p>
     
        
    </div>
    </form>
</body>
</html>

 

 

.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;


public partial class NarrowtToWide : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    

    private void ConvertStrConv()
    {
       

        
        // 不執行轉換
        DisplayList(VbStrConv.None.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.None));       
         
        //將字串轉換為大寫字元。
        DisplayList(VbStrConv.Uppercase.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.Uppercase));       
        //將字串轉換為小寫字元。
        DisplayList(VbStrConv.Lowercase.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.Lowercase));        
        //將字串中每個單字的第一個字母轉換為大寫。     
        DisplayList(VbStrConv.ProperCase.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.ProperCase));        
        //將字串中的半形字元轉換成全形字元。
        DisplayList(VbStrConv.Wide.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.Wide));        
        //將字串中的全形字元轉換成半形字元。
        DisplayList(VbStrConv.Narrow.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.Narrow));        
        ////將字串中的平假名字元轉換成片假名字元。
        //DisplayList(VbStrConv.Katakana.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.Katakana));        
        ////將字串中的片假名字元轉換成平假名字元。
        //DisplayList(VbStrConv.Hiragana.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.Hiragana));        
        //將繁體中文字元轉換成簡體中文。
        DisplayList(VbStrConv.SimplifiedChinese.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.SimplifiedChinese, 2052));        
        //將簡體中文字元轉換成繁體中文。
        DisplayList(VbStrConv.TraditionalChinese.ToString(), Strings.StrConv(txtInput.Text, VbStrConv.TraditionalChinese, 1028));








    }

    private void DisplayList(string strVbStrConvName,string strInValue)
    {   
        Response.Write(String.Format("VbStrConv:{0} 轉換後 {1} <BR/>",strVbStrConvName,strInValue));   
         
        
    }
    protected void btnConvert_Click(object sender, EventArgs e)
    {
        ConvertStrConv();
    }
}

image

測試效果如上圖,透過 Microsoft.VisualBasic  VbStrConv 達成半型互轉全型及簡體轉繁體。

 

參考資源:

Strings.StrConv 方法
http://msdn.microsoft.com/zh-tw/library/microsoft.visualbasic.strings.strconv(VS.80).aspx
LocaleID 查詢表 用於簡繁(正)體轉換使用的 LocaleID
http://msdn.microsoft.com/en-us/library/0h88fahh%28VS.85%29.aspx

Taiwan         zh-tw  0x0404  1028
Chinese China  zh-cn  0x0804  2052