本文實例講述了javascript實現(xiàn)全角轉(zhuǎn)半角的方法。分享給大家供大家參考,具體如下:
function fullChar2halfChar(str){ var result = ''; for (i=0 ; i<str.length; i++) { code = str.charCodeAt(i);//獲取當前字符的unicode編碼 if (code >= 65281 && code <= 65373)//在這個unicode編碼范圍中的是所有的英文字母已經(jīng)各種字符 { result += String.fromCharCode(str.charCodeAt(i) - 65248);//把全角字符的unicode編碼轉(zhuǎn)換為對應(yīng)半角字符的unicode碼 }else if (code == 12288)//空格 { result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32); }else { result += str.charAt(i); } } return result;}希望本文所述對大家JavaScript程序設(shè)計有所幫助。
新聞熱點
疑難解答