本文實例講述了javascript實現全角轉半角的方法。分享給大家供大家參考,具體如下:
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;
}
希望本文所述對大家JavaScript程序設計有所幫助。