• AS3 CookBook学习整理(十一)


    1. 遍历字符串中的字符

    有两种方法实现:

    <1> 直接以char的形式遍历

    package {
     import flash.display.Sprite;
     public class Sample0603 extends Sprite
     {
      public function Sample0603()
      {
       var str:String = "what a beautiful girl";
       
       for(var i:int=0;i<str.length;i++)
       {
        trace(str.charAt(i));
       }
      }
     }
    }

    <2> 先split(""),然后当成一个数组来遍历

    这样的好处在于,可以用数组的系列方法来进行字符的排序,删除等处理

    package {
     import flash.display.Sprite;
     public class Sample0603 extends Sprite
     {
      public function Sample0603()
      {
       var str:String = "what a beautiful girl";
       
       var arr:Array = str.split("");
       
       //arr.sort(); //可以排序
        
       for(var i:int=0;i<arr.length;i++)
       {
        if(arr[i]=="u")
        {
         arr.splice(i,1);
         i--;
        }
       }
       
       trace(arr.join(""));
      }
     }
    }

    2. 大小写转换

    可以利用str.toUpperCase()转换为大写,str.toLowerCase()转换为小写

    可以通过ascb的StringUtilities.toInitialCap(str),将整个字符串的第一个字母变成大写,其它全部小写

    可以通过ascb的StringUtilities.toTitleCase(str),将每个单词(以空格区分)的首字母大写,其它小写

    package {
     import ascb.util.StringUtilities;
     import flash.display.Sprite;
     public class Sample0603 extends Sprite
     {
      public function Sample0603()
      {
       var str:String = "What a beautiful Girl!";
       
       trace(str.toLowerCase());//what a beautiful girl!
        
       trace(str.toUpperCase());//WHAT A BEAUTIFUL GIRL!
        
       trace(StringUtilities.toInitialCap(str));//What a beautiful girl!
        
       trace(StringUtilities.toTitleCase(str));//What A Beautiful Girl! 
      }
     }
    }

    3. 去除首尾空格(Trim)

    有两种方法:

    mx.utils.StringUtil.trim(str)

    ascb.util.StringUtilities.trim(str)

    4. 反转(reverse)字符串

    使用split()方法创建字符数组,然后调用数组的reverse()和join()方法

    package {
     import flash.display.Sprite;
     
     public class Sample0611 extends Sprite
     {
      public function Sample0611()
      {
       var str:String = "God  is  a girl";
       
       //反转字母 
       var arr2:Array = str.split("");
       arr2.reverse();
       trace(arr2.join(""));
       
       //反转单词 
       var arr:Array = str.split(" ");
       arr.reverse();
       trace(arr.join(" "));
      }
     }
    }

    5. 得到字符的Unicode码或ASCII码

    String.fromCharCode(...charCodes) -- 得到当前ASCII码对应的字符

    str.charCodeAt(index) -- 得到当前索引下字符对应的ASCII码值

    package {
     import flash.display.Sprite;
     
     public class Sample0611 extends Sprite
     {
      public function Sample0611()
      {
       trace(String.fromCharCode(65));//A
     
       trace(String.fromCharCode(97,98,99));//ABC
        
       var str:String = "abc";
       trace(str.charCodeAt(0));//97 
      }
     }
    }

    6. 创建一个Date对象

    标准构造函数是:

    public function Date(yearOrTimevalue:Object, month:Number, date:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0, millisecond:Number = 0)

    * 如果未传递参数,则赋予Date对象当前客户端日期和时间

    * 如果传递一个Number数据类型的参数,则基于自GMT时间1970年1月1日 0:00:000以来的毫秒数+该数字

    * 如果传递一个String数据类型的参数,并且该字符串包含一个有效日期,则赋予Date对象此日期和时间,否则返回"Invalid Date"。符合规范的格式如下:

    Day Month Date Hours:Minutes:Seconds GMT Year(例如,“Tue Feb 1 00:00:00 GMT-0800 2005”,这与 toString() 一致)

    Day Month Date Year Hours:Minutes:Seconds AM/PM(例如,“Tue Feb 1 2005 12:00:00 AM”,这与 toLocaleString() 一致)

    Day Month Date Year(例如,“Tue Feb 1 2005”,这与 toDateString() 一致)

    Month/Day/Year(例如,“02/01/2005”)

    Month/Year(例如,“02/2005”)

    package {
     import flash.display.Sprite;
     public class Sample0616 extends Sprite
     {
      public function Sample0616()
      {
       var date:Date = new Date();
       trace(date);//Tue Jun 16 14:44:32 GMT+0800 2009
        
       date = new Date(4000);
       trace(date);//Thu Jan 1 08:00:04 GMT+0800 1970
        
       date = new Date("02/03/1983");
       trace(date);//Thu Feb 3 00:00:00 GMT+0800 1983 
      }
     }
    }

    7. 获取Date对象的信息

    可以通过fullYear、month、date、day、hours、minutes等属性得到Date对象的详细信息

    ASCB下的DateFormat类封装了星期与月份的名称数组,如DAYS, DAYS_ABBREVIATED, MONTHS, 和MONTHS_ABBREVIATED

    PS:和.net不一样,day是返回星期,date返回的才是日期部分。星期日对应的day是0

    package {
     import ascb.util.DateFormat;
     
     import flash.display.Sprite;
     public class Sample0616 extends Sprite
     {
      public function Sample0616()
      {
       var date:Date = new Date();
       
       trace(date); //Tue Jun 16 15:25:18 GMT+0800 2009
      
       trace(date.fullYear); //2009 
       trace(date.month + 1); //6 
       trace(date.date);  //16   
       trace(date.day);  //2 
       trace(date.hours);  //15 
       trace(date.minutes); //25 
       trace(date.seconds); //18 
       trace(date.milliseconds); //毫秒值
     
       trace(DateFormat.DAYS[date.day]); //Tuesday 
       trace(DateFormat.DAYS_ABBREVIATED[date.day]); //Tues 
       trace(DateFormat.MONTHS[date.month]); //June 
       trace(DateFormat.MONTHS_ABBREVIATED[date.month]); //Jun
     
      }
     }
    }

    8. 格式化日期和时间

    利用ascb.util.DateFormat类的format(date)方法来格式化

    package {
     import ascb.util.DateFormat;
     
     import flash.display.Sprite;
     public class Sample0616 extends Sprite
     {
      public function Sample0616()
      {
       var date:Date = new Date();   
       trace(date);//Tue Jun 16 16:35:29 GMT+0800 2009
     
       trace(new DateFormat("Y").format(date));//2009 
       trace(new DateFormat("y").format(date));//09 
       trace(new DateFormat("m").format(date));//06 
       trace(new DateFormat("d").format(date));//16 
       trace(new DateFormat("H:i:s").format(date));//16:35:29 
       trace(new DateFormat("h:i:s").format(date));//04:35:29 
       trace(new DateFormat("h:i:s a").format(date));//04:35:29 pm 
       trace(new DateFormat("Y-m-d 'at' h:i:s a").format(date));//2009-06-16 at 04:48:45 pm 
      }
     }
    }

    9. 格式化秒或毫秒为mm:ss格式

    使用ascb.util.DateFormat.formatSeconds( ) 或ascb.util.DateFormat.formatMilliseconds( )方法

    package {
     import ascb.util.DateFormat;
     
     import flash.display.Sprite;
     public class Sample0616 extends Sprite
     {
      public function Sample0616()
      {
       trace(DateFormat.formatSeconds(301)); //05:01(5分1秒)
        
       trace(DateFormat.formatMilliseconds(68000)); //01:08(1分8秒) 
      }
     }
    }

    10. 创建Sound对象

    要载入声音文件到Sound对象上,需要先创建一个包含mp3文件路径的URLRequest对象,然后调用Sound.load()方法来载入,例如:

    var _sound:Sound = new Sound();

    _sound.load(new URLRequest("song.mp3");

    也可以直接将URLRequest传递给Sound类的构造函数,写法更为简捷:

    var _sound:Sound = new Sound(new URLRequest("song.mp3"));

    注意:

    * 如果是以构造函数的形式创建Sound对象,则自动调用Sound对象的load()方法;否则需要自己调用load()方法

    * 一旦对某个Sound对象调用了load(),就不能再将另一个声音文件加载到该Sound对象中。若要加载另一个声音文件,请创建新的Sound对象

  • 相关阅读:
    poj2181 jumping cow
    poj2184
    POJ 1189 钉子和小球
    uva11019矩阵匹配器D316
    noip2015运输计划
    [LintCode] Last Position of Target
    [LintCode] K Closest Points
    [LintCode] K Closest Numbers In Sorted Array
    [LintCode] Closest Number in Sorted Array
    [LintCode] Perfect Squares
  • 原文地址:https://www.cnblogs.com/CoderWayne/p/1778072.html
Copyright © 2020-2023  润新知