Uli
Uli

Reputation: 2693

AS3 - Capitalize every word in a sentence

How to capitalize the first letter of every word in a sentence? (Similar to CSS text-transform capitalize)

Thank you. Uli

Upvotes: 2

Views: 2374

Answers (2)

Arno van Oordt
Arno van Oordt

Reputation: 3520

Use a regular expression replace:

var str:String = "the quick brown fox jumped over the lazy dog.";
str = str.replace(/(^[a-z]|\s[a-z])/g, function():String{ return arguments[1].toUpperCase(); });

Upvotes: 6

Cyclonecode
Cyclonecode

Reputation: 30061

Something like this should work:

function ucfirst(str:String):String {
   var words:Array = str.split(" ");
   for(var i in words) {
       words[i] = String(words[i]).charAt(0).toUpperCase() + String(words[i]).substr(1, String(words[i]).length);
   }
   return words.join(" ");
}

Upvotes: 1

Related Questions