Home > Actionscript, Flash, Flex > Convert any string in TitleCase using ActionScript Code

Convert any string in TitleCase using ActionScript Code

 
var inputString:String =”jay patel”
var resultString:String;

var newTxtArr:Array;

txtArr = new Array();

var txtArr:Array = inputString.toLowerCase().split(“<br>”);

resultString = txtArr.join(” “);

 

var txtArr:Array = resultString.split(” “);

newTxtArr = new Array();

for (var i = 0; i<txtArr.length; i++) {

resultString = txtArr[i].substr(0, 1).toUpperCase()+

                                          txtArr[i].substr(1, (txtArr[i].length)).toLowerCase();

newTxtArr.push(resultString);

}

resultString = newTxtArr.join(” “);

trace(resultString)

 

Advertisements
  1. Vince
    February 27, 2009 at 2:24 PM

    or, a little more compact:
    resultString = inputString.split(" ").map(function(element:String, index:int, arr:Array):String{
    return element.substr(0, 1).toLocaleUpperCase() + element.substr(1);
    }).join(" ");

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: