【AS3实现经典算法统计字符串中数字、英文字母、空格和其它字符的个数】

题目:分别统计字符串中英文字母、空格、数字和其它字符的个数。

  1.程序分析:利用if-else语句,条件为输入的字符不为 '\n '

  */

  import flash.display.Sprite;

  public class CharacterCount extends Sprite{

  private var digital:int = 0; //数字

  private var character:int = 0; //字母

  private var space:int = 0; //空格

  private var other:int = 0; //其它字符

  public function CharacterCount(){

  var str:String = "23rARC0892 the name is OK! _Chacher Hello!!!";

  init(str);

  }

  private function init(str:String):void{

  for(var i:int=0; i<str.length; i++){

  if(str.charAt(i)>='0' && str.charAt(i)<='9'){

  digital++;

  }else if((str.charAt(i)>='a' && str.charAt(i)<='z') || (str.charAt(i)>='A' && str.charAt(i) <= 'Z')){

  character++;

  }else if(str.charAt(i)==' '){

  space++;

  }else{

  other++;

  }

  }

  trace("digital : " + digital); //数字

  trace("character : " + character); //字母

  trace("space : " + space); //空格

  trace("other : " + other); //其它字符

  }

  }

  }

  /* output

  digital : 6

  character : 27

  space : 6

  other : 5

  */

posted on 2013-09-04 09:46  挖掘者者者  阅读(320)  评论(0编辑  收藏  举报