String.prototype.ucfirst = function()
{
// Split the string into words if string contains multiple words.
var x = this.split(/\s+/g);
for(var i = 0; i < x.length; i++)
{
// Splits the word into two parts. One part being the first letter,
// second being the rest of the word.
var parts = x[i].match(/(\w)(\w*)/);
// Put it back together but uppercase the first letter and lowercase the rest of thw word.
x[i] = parts[1].toUpperCase() + parts[2].toLowerCase();
}
// Rejoin the string and return.
return x.join(' ');
};
Refactorings
No refactoring yet !
txen
October 9, 2007, October 09, 2007 08:47, permalink
I think this is a cleaner code... there isn't regular expresions...
the ucfirst usually doesn't lowerCase the rest of the string but if you want you can write a .toLowerCase() in the last substring..
String.prototype.ucfirst = function()
{
return this.substring(0,1).toUpperCase()+this.substring(1);
};
txen
October 9, 2007, October 09, 2007 09:48, permalink
ups, sorry. I think you wanted that, (it's my fault)
String.prototype.ucfirst = function()
{
var text;
text = this[0].toUpperCase();
for(var i = 1; i<this.length; i++){
text += this[i-1]==' ' ? this[i].toUpperCase(): this[i].toLowerCase() ;
}
return text;
};
Andre Steenveld
October 9, 2007, October 09, 2007 11:59, permalink
txen; your soloution is quite nice but with really long string concatting is quite slow. Usually an array is quite a bit faster. Your first soloution was quite nice just needed to wrap it in a for() loop =)
String.prototype.ucFirst = function(){
var words = this.split(" ");
for( var i = 0; i < words.length; i++ ){
words[i] = !words[i].length ?
words[i] : words[i].substring(0, 1).toUpperCase() + words[i].substring(1);
}
return words.join(" ");
}
// Were doing a little check on line 4 because what is going to happen when we call ucFirst on something like this?
" ".ucFirst();
Andre Steenveld
October 9, 2007, October 09, 2007 12:09, permalink
Just a little fix
String.prototype.ucFirst = function(){
var words = this.split(" ");
for( var i = 0; i < words.length; i++ ){
words[i] = words[i].length === 0 || words[i].length === 1 ?
words[i].toUpperCase() : ( words[i].substring(0, 1).toUpperCase() + words[i].substring(1) );
}
return words.join(" ");
}
travis
October 9, 2007, October 09, 2007 14:51, permalink
There is a way to do this with CSS, in case you don't specifically need to do it with JS: http://www.w3.org/TR/REC-CSS2/text.html#caps-prop
#someSelector
{
text-transform: capitalize;
}
Emmett
October 9, 2007, October 09, 2007 15:51, permalink
travis: Now that's what I call real refactoring.
If you're hellbent on doing it in JS though...String.replace is pretty much infinitely powerful.
//usage: "my sentence: cats, dogs, and fish".capitalize_all() == "My Sentence: Cats, Dogs, And Fish"
String.prototype.capitalize_all = function(){
return this.replace(/(\w+)/g, function(word){
return word.capitalize();
});
}
//usage: "my word".capitalize() == "My word"
String.prototype.capitalize = function(){
return this.replace(/\w/, function(first_letter){
return first_letter.toUpperCase();
});
}
Felipe Alcacibar
October 22, 2010, October 22, 2010 22:48, permalink
like the avobe function but with more efficient regexp
// ucfirst all the words
var ucfirst = function() {
return s.replace(/(^|\s+?)[a-z]/mg, function(s) {; return s.toUpperCase(); })
});
I'm wondering if this could be made better.