如何将句子中每个单词的首字母大写? (类似于CSS text-transform capitalize )
谢谢。 乌利
答案 0 :(得分:6)
使用正则表达式替换:
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(); });
答案 1 :(得分:1)
这样的事情应该有效:
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(" ");
}