Flash / Flex / ActionScript/String/case

Материал из Web эксперт
Перейти к: навигация, поиск

Changing the Case of a String

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript");
        trace(sTitle.toLowerCase());  
        trace(sTitle.toUpperCase());  
        trace(sTitle.valueOf());  
    }
  }
}
actionscript
ACTIONSCRIPT
ActionScript



Converting Case

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "What case?";
        
        trace( example.toLowerCase(  ) );
        
        trace( example.toUpperCase(  ) );
        
        trace( example );
    }
  }
}



Convert string to lower case then call the lastIndexOf method

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "Cool. This is a cool as both cool (lowercase) and Cool.";
        
        var search:String = "cool";
        
        
        trace( example.toLowerCase(  ).lastIndexOf( search ) );
    }
  }
}



Initial capitalizing the string

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        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(  );
    }
  }
}



Is lastIndexOf case sensitive

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var example:String = "Cool. This is a cool as both cool (lowercase) and Cool.";
        
        var search:String = "cool";
        
        trace( example.lastIndexOf( search ) );
    }
  }
}



The toLowerCase() and toUpperCase() methods are particularly useful for comparing strings in a case-insensitive manner.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitleOne:String = new String("ActionScript Bible");
        var sTitleTwo:String = new String("ActionScript bible");
        trace(sTitleOne.valueOf() == sTitleTwo.valueOf());
        trace(sTitleOne.toUpperCase() == sTitleTwo.toUpperCase());
    }
  }
}



To alter the original string, reassign the return value to it, as follows:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "What case?";
        example = example.toLowerCase(  );
    }
  }
}



toTitleCase( ) converts a string to title case (initial letters capitalized)

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "the actionScript cookbook";
        
        trace( toTitleCase( example ) );

    }
    public static function toTitleCase( original:String ):String {
      var words:Array = original.split( " " );
      for (var i:int = 0; i < words.length; i++) {
        words[i] = toInitialCap( words[i] );
      }
      return ( words.join( " " ) );
    }
    public static function toInitialCap( original:String ):String {
      return original.charAt( 0 ).toUpperCase(  ) + original.substr( 1 ).toLowerCase(  );
    }    
  }
}



Using toUpperCase() or toLowerCase() to make sure that two strings are compared without regard to case

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
         var inputPW:String = "HaXoR"
         var storedPW:String = "haxor";
        if (storedPW.toLowerCase() == inputPW.toLowerCase()) {
             trace("Login successful");
        } else {
            trace("Login error");
        }
    }
  }
}