Reputation: 2693
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
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
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