全角字符转换为半角
方法一:
方法二:
添加 Microsoft Visual Basic.NET Runtime 引用。
Console.WriteLine(Microsoft.VisualBasic.Strings.StrConv("abcdefg", VbStrConv.Narrow, 0));
---------------------------------------------------
this.txt.Attributes.Add("onchange","WideCharToChar()");
js实现半全角转换
function WideCharToChar(){
c =event.srcElement.value;
myArray= new Array();
for(i=0;i<c.length;i++)
{
if(c.charCodeAt!=null&&String.fromCharCode!=null)
{
var charCode=c.charCodeAt(i);
if(charCode>0xfee0){
myArray[i]=String.fromCharCode(charCode-0xfee0);
}
else
myArray[i]=String.fromCharCode(charCode);
}
}
event.srcElement.value=str;
}
---------------------------------------------------
下面这段代码就是全角转半角的
public static string CharConverter(string source)
{
System.Text.StringBuilder result = new System.Text.StringBuilder(source.Length, source.Length);
for (int i=0; i<source.Length; i++)
{
if (source[i] >= 65281 && source[i] <= 65373)
{
result.Append((char)(source[i] - 65248));
}
else if (source[i] == 12288)
{
result.Append(' ');
}
else
{
result.Append(source[i]);
}
}
return result.ToString();
}
/// <summary>
/// 判断字符是否英文半角字符或标点
/// </summary>
/// <remarks>
/// 32 空格
/// 33-47 标点
/// 48-57 0~9
/// 58-64 标点
/// 65-90 A~Z
/// 91-96 标点
/// 97-122 a~z
/// 123-126 标点
/// </remarks>
public static bool IsBjChar(char c)
{
int i = (int)c;
return i >= 32 && i <= 126;
}
/// <summary>
/// 判断字符是否全角字符或标点
/// </summary>
/// <remarks>
/// <para>全角字符 - 65248 = 半角字符</para>
/// <para>全角空格例外</para>
/// </remarks>
public static bool IsQjChar(char c)
{
if (c == '\u3000') return true;
int i = (int)c - 65248;
if (i < 32) return false;
return IsBjChar((char)i);
}
/// <summary>
/// 将字符串中的全角字符转换为半角
/// </summary>
public static string ToBj(string s)
{
if (s == null || s.Trim() == string.Empty) return s;
StringBuilder sb = new StringBuilder(s.Length);
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '\u3000')
sb.Append('\u0020');
else if (IsQjChar(s[i]))
sb.Append((char)((int)s[i] - 65248));
else
sb.Append(s[i]);
}
return sb.ToString();
}
/// 判断字符是否英文半角字符或标点
/// </summary>
/// <remarks>
/// 32 空格
/// 33-47 标点
/// 48-57 0~9
/// 58-64 标点
/// 65-90 A~Z
/// 91-96 标点
/// 97-122 a~z
/// 123-126 标点
/// </remarks>
public static bool IsBjChar(char c)
{
int i = (int)c;
return i >= 32 && i <= 126;
}
/// <summary>
/// 判断字符是否全角字符或标点
/// </summary>
/// <remarks>
/// <para>全角字符 - 65248 = 半角字符</para>
/// <para>全角空格例外</para>
/// </remarks>
public static bool IsQjChar(char c)
{
if (c == '\u3000') return true;
int i = (int)c - 65248;
if (i < 32) return false;
return IsBjChar((char)i);
}
/// <summary>
/// 将字符串中的全角字符转换为半角
/// </summary>
public static string ToBj(string s)
{
if (s == null || s.Trim() == string.Empty) return s;
StringBuilder sb = new StringBuilder(s.Length);
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '\u3000')
sb.Append('\u0020');
else if (IsQjChar(s[i]))
sb.Append((char)((int)s[i] - 65248));
else
sb.Append(s[i]);
}
return sb.ToString();
}
方法二:
添加 Microsoft Visual Basic.NET Runtime 引用。
Console.WriteLine(Microsoft.VisualBasic.Strings.StrConv("abcdefg", VbStrConv.Narrow, 0));
---------------------------------------------------
this.txt.Attributes.Add("onchange","WideCharToChar()");
js实现半全角转换
function WideCharToChar(){
c =event.srcElement.value;
myArray= new Array();
for(i=0;i<c.length;i++)
{
if(c.charCodeAt!=null&&String.fromCharCode!=null)
{
var charCode=c.charCodeAt(i);
if(charCode>0xfee0){
myArray[i]=String.fromCharCode(charCode-0xfee0);
}
else
myArray[i]=String.fromCharCode(charCode);
}
}
event.srcElement.value=str;
}
---------------------------------------------------
下面这段代码就是全角转半角的
public static string CharConverter(string source)
{
System.Text.StringBuilder result = new System.Text.StringBuilder(source.Length, source.Length);
for (int i=0; i<source.Length; i++)
{
if (source[i] >= 65281 && source[i] <= 65373)
{
result.Append((char)(source[i] - 65248));
}
else if (source[i] == 12288)
{
result.Append(' ');
}
else
{
result.Append(source[i]);
}
}
return result.ToString();
}