Home  >  Article  >  Web Front-end  >  asp (javascript) full-width and half-width conversion code dbc2sbc_javascript skills

asp (javascript) full-width and half-width conversion code dbc2sbc_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:48:261372browse
asp full-width and half-width conversion function
When flag=-1, half-width to full-width is performed
When flag=0, half-width and full-width conversion are performed
When flag=1, full-width to half-width is converted
Copy code The code is as follows:

<%
Function DBC2SBC(Str, flag)
Dim i, sStr
If Len(Str)<= 0 Then Exit Function
DBC2SBC = ""
For i = 1 To Len(Str)
sStr = Asc(Mid(Str, i , 1))
Select Case flag
Case -1
If sStr>0 And sStr<= 125 Then
DBC2SBC = DBC2SBC & Chr(Asc(Mid(Str, i, 1)) - 23680)
Else
DBC2SBC = DBC2SBC & Mid(Str, i, 1)
End If
Case 0
If sStr>0 And sStr<= 125 Then
DBC2SBC = DBC2SBC & Chr(Asc(Mid(Str, i, 1)) -23680)
Else
DBC2SBC = DBC2SBC & Chr(Asc(Mid(Str, i, 1)) 23680)
End If
Case 1
If sStr<0 Or sStr>125 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 version full-width and half-width conversion function
Copy code The code is as follows:

function dbc2sbc(sStr){
var dbc2sbc = sStr;
for (var i = 65281; i < 65375; 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 common function sbc2dbc()
Copy code The code is as follows:

function sbc2dbc(sStr){
var sbc2dbc = sStr;
for (var i = 33; i < 127; 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;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn