forked from limingziqiang/functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchgCase.js
34 lines (33 loc) · 1.06 KB
/
chgCase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @description 全角半角转换
* @author 未知
*/
//iCase: 0全到半,1半到全,其他不转化
function chgCase(sStr,iCase){
if(typeof sStr != "string" || sStr.length <= 0 || !(iCase === 0 || iCase == 1)){
return sStr;
}
var i,oRs=[],iCode;
if(iCase){/*半->全*/
for(i=0; i<sStr.length;i+=1){
iCode = sStr.charCodeAt(i);
if(iCode == 32){
iCode = 12288;
}else if(iCode < 127){
iCode += 65248;
}
oRs.push(String.fromCharCode(iCode));
}
}else{/*全->半*/
for(i=0; i<sStr.length;i+=1){
iCode = sStr.charCodeAt(i);
if(iCode == 12288){
iCode = 32;
}else if(iCode > 65280 && iCode < 65375){
iCode -= 65248;
}
oRs.push(String.fromCharCode(iCode));
}
}
return oRs.join("");
}