JavaScript查看拼音首字母缩写


上面的程序是输出所有汉字的大写首字母,要是只要求输出第一个汉字的首字母,可以把function makePy(str){}函数改成下面的样子。

function makePy(str){
    if(typeof(str) != "string")
        throw new Error(-1,"函数makePy需要字符串类型参数!");
    var arrResult = new Array();
    for(var i=0;i<1;i++){
        var ch = str.charAt(i);
        arrResult.push(checkCh(ch));
    }
    return mkRslt(arrResult);
}

该程序只处理了中文字符,对与非中文字符直接输出,如果要求字母输入大写、汉字输出首字母、特殊字符输出#,可以改成下面的样子。

function checkCh(ch){//如果字母返回大写,如果特殊字符返回#,否则进行汉字处理
    var uni = ch.charCodeAt(0);
    if(uni > 40869 || uni < 19968){
if(ch=='a' || ch=='A'){ return 'A'; }
else if(ch=='b' || ch=='B'){ return 'B'; }
else if(ch=='c' || ch=='C'){ return 'C'; }
else if(ch=='d' || ch=='D'){ return 'D'; }
else if(ch=='e' || ch=='E'){ return 'E'; }
else if(ch=='f' || ch=='F'){ return 'F'; }
else if(ch=='g' || ch=='G'){ return 'G'; }
else if(ch=='h' || ch=='H'){ return 'H'; }
else if(ch=='i' || ch=='I'){ return 'I'; }
else if(ch=='j' || ch=='J'){ return 'J'; }
else if(ch=='k' || ch=='K'){ return 'K'; }
else if(ch=='l' || ch=='L'){ return 'L'; }
else if(ch=='m' || ch=='M'){ return 'M'; }
else if(ch=='n' || ch=='N'){ return 'N'; }
else if(ch=='o' || ch=='O'){ return 'O'; }
else if(ch=='p' || ch=='P'){ return 'P'; }
else if(ch=='q' || ch=='Q'){ return 'Q'; }
else if(ch=='r' || ch=='R'){ return 'R'; }
else if(ch=='s' || ch=='S'){ return 'S'; }
else if(ch=='t' || ch=='T'){ return 'T'; }
else if(ch=='u' || ch=='U'){ return 'U'; }
else if(ch=='v' || ch=='V'){ return 'V'; }
else if(ch=='w' || ch=='W'){ return 'W'; }
else if(ch=='x' || ch=='X'){ return 'X'; }
else if(ch=='y' || ch=='Y'){ return 'Y'; }
else if(ch=='z' || ch=='Z'){ return 'Z'; }
        else{ return '#'; }
}
else{
    	return (strChineseFirstPY.charAt(uni-19968));
}
}
版权声明:若无特殊注明,本文皆为( VISAce )收集整理,版权归原作者所有。—— 李佳培.中国

暧昧贴

发表评论

      avatar
      • 微笑 大笑 拽 大哭 奸笑 流汗 喷血 生气 囧 不爽 晕 示爱 害羞 吃惊 惊叹 爱你 吓死了 呵呵
      0
      假装提交肿...



      既然没有评论,那就赶紧抢沙发吧!