当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript数字全角半角转换代码教程

JavaScript数字全角半角转换代码教程

2019年03月23日  | 移动技术网IT编程  | 我要评论

javascript数字全角半角转换代码教程

</pre><pre name="code" class="javascript">///全角空格为12288,半角空格为32   
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248   
//半角转换为全角函数   
function todbc(txtstring)   
{   
var tmp = "";   
    for(var i=0;i<txtstring.length;i++)   
    {   
        if(txtstring.charcodeat(i)==32)   
        {   
            tmp= tmp+ string.fromcharcode(12288);   
        }   
        if(txtstring.charcodeat(i)<127)   
        {   
            tmp=tmp+string.fromcharcode(txtstring.charcodeat(i)+65248);   
        }   
    }   
    return tmp;   
}   
//全角转换为半角函数   
function tocdb(str)   
{   
    var tmp = "";   
    for(var i=0;i<str.length;i++)   
    {   
        if(str.charcodeat(i)>65248&&str.charcodeat(i)<65375){   
            tmp += string.fromcharcode(str.charcodeat(i)-65248);   
        }else {   
            tmp += string.fromcharcode(str.charcodeat(i));   
        }   
    }   
    return tmp   
}  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网