asp(javascript)全形半形轉碼 dbc2sbc

來源:互聯網
上載者:User

asp全形半形轉換函式
flag=-1時進行半形轉全形
flag=0時進行半形全形互轉
flag=1時進行全形轉半形 複製代碼 代碼如下:<%
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版 全形半形轉換函式
複製代碼 代碼如下: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常用函數 sbc2dbc()
複製代碼 代碼如下: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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.