toTitleCase( ) converts a string to title case (initial letters capitalized) : case « String « Flash / Flex / ActionScript






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(  );
    }    
  }
}

        








Related examples in the same category

1.Converting Case
2.Changing the Case of a String
3.The toLowerCase() and toUpperCase() methods are particularly useful for comparing strings in a case-insensitive manner.
4.Is lastIndexOf case sensitive
5.Convert string to lower case then call the lastIndexOf method
6.To alter the original string, reassign the return value to it, as follows:
7.Initial capitalizing the string
8.Using toUpperCase() or toLowerCase() to make sure that two strings are compared without regard to case