web页面全角&半角

原文链接: https://www.mk2048.com/blog/blog.php?id=h0jai1ibhc1j&title=web%E9%A1%B5%E9%9D%A2%E5%85%A8%E8%A7%92%26%E5%8D%8A%E8%A7%92

根据Unicode编码,全角空格为12288,半角空格为32 ;

其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 

  • 全角-->半角函数 
//半角转换为全角函数 
function ToBCD(str) { 
    var tmp = ""; 
    for(var i=0;i<str.length;i  ) {
     var code = str.charCodeAt(i);//获取当前字符的Unicode编码
        if(code>=65281 && code<=65373) { //Unicode编码中所有的全角英文字符以及各种字符
            tmp  = String.fromCharCode(str.charCodeAt(i)-65248); 
        } else if(code==12288) {//空格
            tmp  = String.fromCharCode(str.charCodeAt(i)-12256);
        } else { 
            tmp  = String.fromCharCode(str.charCodeAt(i)); 
        } 
    } 
    return tmp 
}                

更多专业前端知识,请上 【猿2048】www.mk2048.com

猜你喜欢

转载自blog.csdn.net/weixin_39037804/article/details/102741755