﻿ 轉全角半角(C#，VB.NET)_電腦知識網

# 轉全角半角(C#，VB.NET)

2022-06-13   來源: .NET編程

/// 轉全角的函數(SBC case) ///
///

任意字符串

/// 全角字符串 ///
///全角空格為半角空格為
///其他字符半角()與全角()的對應關系是均相差 ///

public string ToSBC(string input)
{ //半角轉全角
char[] c=inputToCharArray();
for (int i = ; i < cLength; i++)
{
if (c[i]==)
{
c[i]=(char); continue;
}
if (c[i]<) c[i]=(char)(c[i]+);
}
return new string(c);
}

///

/// 轉半角的函數(DBC case) ///
///

任意字符串

/// 半角字符串 ///
///全角空格為半角空格為
///其他字符半角()與全角()的對應關系是均相差 ///

public string ToDBC(string input)
{
char[] c=inputToCharArray();
for (int i = ; i < cLength; i++)
{
if (c[i]==)
{
c[i]= (char); continue;
}
if (c[i]> && c[i]<)
c[i]=(char)(c[i]);
}
return new string(c);
}

VB版本

Public Function ToSBC()Function ToSBC(ByVal input As String) As String 轉全角 Dim c As Char() = inputToCharArray Dim i As Integer = While i < cLength If c(i) = ChrW() Then c(i) = ChrW() ElseIf AscW(c(i)) < Then c(i) = ChrW((AscW(c(i)) + )) End If SystemMathMin(SystemThreadingInterlockedIncrement(i) i ) End While Return New String(c) End Function Public Function ToDBC()Function ToDBC(ByVal input As String) As String 轉半角 Dim c As Char() = inputToCharArray Dim i As Integer = While i < cLength If AscW(c(i)) = Then c(i) = ChrW() ElseIf AscW(c(i)) > AndAlso AscW(c(i)) < Then c(i) = ChrW(AscW(c(i)) ) End If SystemMathMin(SystemThreadingInterlockedIncrement(i) i ) End While Return New String(c) End Function

From:http://tw.wingwit.com/Article/program/net/201311/11902.html
• 上一篇文章：

• 下一篇文章：