Skip to content

Commit 4975d57

Browse files
committed
全角半角转换
1 parent 3548415 commit 4975d57

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

chgCase.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @description 全角半角转换
3+
* @author 未知
4+
*/
5+
6+
//iCase: 0全到半,1半到全,其他不转化
7+
function chgCase(sStr,iCase){
8+
if(typeof sStr != "string" || sStr.length <= 0 || !(iCase === 0 || iCase == 1)){
9+
return sStr;
10+
}
11+
var i,oRs=[],iCode;
12+
if(iCase){/*半->全*/
13+
for(i=0; i<sStr.length;i+=1){
14+
iCode = sStr.charCodeAt(i);
15+
if(iCode == 32){
16+
iCode = 12288;
17+
}else if(iCode < 127){
18+
iCode += 65248;
19+
}
20+
oRs.push(String.fromCharCode(iCode));
21+
}
22+
}else{/*全->半*/
23+
for(i=0; i<sStr.length;i+=1){
24+
iCode = sStr.charCodeAt(i);
25+
if(iCode == 12288){
26+
iCode = 32;
27+
}else if(iCode > 65280 && iCode < 65375){
28+
iCode -= 65248;
29+
}
30+
oRs.push(String.fromCharCode(iCode));
31+
}
32+
}
33+
return oRs.join("");
34+
}

0 commit comments

Comments
 (0)