Home >Web Front-end >JS Tutorial >JavaScript method to achieve full-width half-width conversion_javascript skills

JavaScript method to achieve full-width half-width conversion_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:18:381622browse

The example in this article describes the method of realizing full-width half-width conversion in JavaScript. Share it with everyone for your reference, the details are as follows:

function fullChar2halfChar(str)
{
 var result = '';
 for (i=0 ; i<str.length; i++)
 {
 code = str.charCodeAt(i);//获取当前字符的unicode编码
 if (code >= 65281 && code <= 65373)//在这个unicode编码范围中的是所有的英文字母已经各种字符
 {
  result += String.fromCharCode(str.charCodeAt(i) - 65248);//把全角字符的unicode编码转换为对应半角字符的unicode码
 }else if (code == 12288)//空格
 {
  result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
 }else
 {
  result += str.charAt(i);
 }
 }
 return result;
}

I hope this article will be helpful to everyone in JavaScript programming.

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