首页 >web前端 >js教程 >asp(javascript)全角半角转换代码 dbc2sbc_javascript技巧

asp(javascript)全角半角转换代码 dbc2sbc_javascript技巧

WBOY
WBOY原创
2016-05-16 18:48:261416浏览
asp全角半角转换函数
flag=-1时进行半角转全角
flag=0时进行半角全角互转
flag=1时进行全角转半角
复制代码 代码如下:

Function DBC2SBC(Str, flag)
Dim i, sStr
If Len(Str)DBC2SBC = ""
For i = 1 To Len(Str)
sStr = Asc(Mid(Str, i, 1))
Select Case flag
Case -1
If sStr>0 And sStrDBC2SBC = DBC2SBC & Chr(Asc(Mid(Str, i, 1)) -23680)
Else
DBC2SBC = DBC2SBC & Mid(Str, i, 1)
End If
Case 0
If sStr>0 And sStrDBC2SBC = DBC2SBC & Chr(Asc(Mid(Str, i, 1)) -23680)
Else
DBC2SBC = DBC2SBC & Chr(Asc(Mid(Str, i, 1)) + 23680)
End If
Case 1
If sStr125 Then
DBC2SBC = DBC2SBC & Chr(Asc(Mid(Str, i, 1)) + 23680)
Else
DBC2SBC = DBC2SBC & Mid(Str, i, 1)
End If
End Select
Next
End Function
%>

javascript版 全角半角转换函数
复制代码 代码如下:

function dbc2sbc(sStr){
var dbc2sbc = sStr;
for (var i = 65281; i var re = new RegExp(String.fromCharCode(i), "g");
var va = String.fromCharCode(i - 65248);
dbc2sbc = dbc2sbc.replace(re, va);
}
dbc2sbc = dbc2sbc.replace(/ /g, ' ');
return dbc2sbc;
}

Javascript常用函数 sbc2dbc()
复制代码 代码如下:

function sbc2dbc(sStr){
var sbc2dbc = sStr;
for (var i = 33; i stringFromCharCode = String.fromCharCode(i);
switch (stringFromCharCode) {
case "(":
case ")":
case "*":
case "+":
case "/":
case "\\":
case "[":
case "]":
case "?":
case "$":
case ".":
case "^":
case "|":
stringFromCharCode = '\\' + stringFromCharCode;
default:
break;
}
var re = new RegExp(stringFromCharCode, "g");
var va = String.fromCharCode(i + 65248);
sbc2dbc = sbc2dbc.replace(re, va);
}
sbc2dbc = sbc2dbc.replace(/ /g, ' ');
return sbc2dbc;
}
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn