as3基础代码系列2-String大小写转换

1.调用String类的toLowerCase方法和toUpperCase方法大小写转换:

package    
{
  import flash.display.Sprite;
  
   /**
    * 字符串大小写转换
    * @author xcf007
    */
   public class case1 extends Sprite
  {
    
     public function case1()
    {
      var example:String = "What case?";
      trace(example.toLowerCase()); //转化为小写
      trace(example.toUpperCase()); //转化为大写
      trace(example.valueOf());      
    }
    
  }
  
}

2.不区分大小写的比较:

       //不区分大小写的比较
      var sTitleOne:String = new String( "ActionScript Bible");
      var sTitleTwo:String = new String( "ActionScript bible");
      trace(sTitleOne.valueOf() == sTitleTwo.valueOf()); //false
      trace(sTitleOne.toUpperCase() == sTitleTwo.toUpperCase()); //true

3.lastIndexOf从右向左查找第一次匹配结果位置:
      var example:String = "Cool. This is a cool as both cool (lowercase) and Cool.";
      var search:String = "cool";
      trace(example.lastIndexOf(search)); //最后一次匹配的首字符索引位置
若不区分大小写的查找可以提前调用toLowerCase()方法做个大小写转换后查找
trace( example.toLowerCase(  ).lastIndexOf( search ) );

4.首字母大写形式:

     public function case1()
    {
      var example:String = "bRuCE";
      trace(toInitialCap(example)); // 首字母大写形式,Displays: Bruce
    }
     public static function toInitialCap( original:String ):String {
         return original.charAt(0).toUpperCase() + original.substr(1).toLowerCase();
    }  
这里String.charAt()返回参数位置指定的字符。String.substr()返回子串。

源地址: http://www.java2s.com/Code/Flash-Flex-ActionScript/String/ConvertingCase.htm

你可能感兴趣的:(职场,as3,休闲,toUpperCase,toLowerCase)