- 论坛徽章:
- 1
|
提供一个js或者vs版本的,绝对好用,把下面的代码直接拷贝到test.html,查看就可以看到绝好的实际效果。
- <script>
- /*
- ****************************
- * 半角<=>全角 *
- * [NB联盟] *
- * Writer YuPing *
- ****************************
- * 参数说明:
- * str:要转换的字符串
- * flag:标记,为0时半转全,为非0时全转半
- * 返回值类型:字符串
- ****************************
- */
- function DBC2SBC(str,flag) {
- var i;
- var result='';
- if (str.length<=0) {alert('字符串参数出错');return false;}
- for(i=0;i<str.length;i++)
- { str1=str.charCodeAt(i);
- if(str1<125&&!flag)
- result+=String.fromCharCode(str.charCodeAt(i)+65248);
- else
- result+=String.fromCharCode(str.charCodeAt(i)-65248);
- }
- return result;
- }
- //示例:
- alert(DBC2SBC("AAabc",0));
- document.write(DBC2SBC("abcdefg",0))
- </script>
- <input type=text value="abcdefg" id=txt><input type=button value="变" onclick=txt.value=DBC2SBC(txt.value)>
- <script language="vbscript">
- '****************************
- '* 半角<=>全角 *
- '* [NB联盟] *
- '* Writer YuPing *
- '****************************
- '* 参数说明:
- '* str:要转换的字符串
- '* flag:标记,为0时半转全,为非0时全转半
- '* 返回值类型:字符串
- '****************************
- function DBC2SBC(str,flag)
- dim i
- if len(str)<=0 then
- msgbox '字符串参数出错'
- exit function
- end if
- for i=1 to len(str)
- str1=asc(mid(str,i,1))
- if str1>0 and str1<=125 and not flag then
- dbc2sbc=dbc2sbc&chr(asc(mid(str,i,1))-23680)
- else
- dbc2sbc=dbc2sbc&chr(asc(mid(str,i,1))+23680)
- end if
- next
- end function
- '示例:
- alert(dbc2sbc("AB",1))
- </script>
复制代码 |
|