题目:将字符串做下面的操作,字符串里面的英文字母都变成它的下一个字符,数字及其他的字符不变:
下面是转化函数:
private function converTo(firstStr:String):String
{
var secondStr:String = "";
var length:int = firstStr.length;
var chatN:Number = 0;
for(var i:int=0;i<length;i++)
{
chatN = firstStr.charCodeAt(i);
//将小写的z转化成a
if(chatN==90)
{
secondStr += String.fromCharCode(65);
}
//将大写的Z转化成A
else if(chatN == 122)
{
secondStr += String.fromCharCode(97);
}
//将字母转化成它的下一个
else if(((chatN >=65) && (chatN < 90)) || ((chatN >= 97) && (chatN < 122)))
{
secondStr += String.fromCharCode(chatN + 1);
}
//其他的字符不变
else
{
secondStr += firstStr.charAt(i);
}
}
return secondStr;
}
测试为:
var tempStr:String = "A5689BCDZabcdefgh8888ijklm1236n";
var converStr:String = this.converTo(tempStr);
trace(tempStr); 输出:A5689BCDZabcdefgh8888ijklm1236n
trace(converStr); 输出:B5689CDEAbcdefghi8888jklmn1236o