網(wǎng)(LieHuo.Net)教程 在工作中用到將全角字符轉換成半角的地方,就找了這個轉換函數(shù)。
以下為引用的內容: //str:要轉換的字符串 function DBC2SBC(str) { var result=""; for(var i=0;i<str.length;i++) { code = str.charCodeAt(i);//獲取當前字符的unicode編碼 if (code >= 65281 && code <= 65373)//在這個unicode編碼范圍中的是所有的英文字母已經各種字符 { var d=str.charCodeAt(i)-65248; result += String.fromCharCode(d);//把全角字符的unicode編碼轉換為對應半角字符的unicode碼 } else if (code == 12288)//空格 { var d=str.charCodeAt(i)-12288+32; result += String.fromCharCode(d); } else { result += str.charAt(i); } } return result; } |