90dfcf7e54299842383ab503b6df1a65

I'm wondering if this could be made better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 !

3623af0337de1b64ced5742cc221d03a

txen

October 9, 2007, October 09, 2007 08:47, permalink

1 rating. Login to rate!

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..

1
2
3
4
String.prototype.ucfirst = function()
{
	return this.substring(0,1).toUpperCase()+this.substring(1);
};
3623af0337de1b64ced5742cc221d03a

txen

October 9, 2007, October 09, 2007 09:48, permalink

No rating. Login to rate!

ups, sorry. I think you wanted that, (it's my fault)

1
2
3
4
5
6
7
8
9
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;
};
7ab05ae02bbc7401f852d586bba1afc2

Andre Steenveld

October 9, 2007, October 09, 2007 11:59, permalink

No rating. Login to rate!

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 =)

1
2
3
4
5
6
7
8
9
10
11
12
13
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();
7ab05ae02bbc7401f852d586bba1afc2

Andre Steenveld

October 9, 2007, October 09, 2007 12:09, permalink

No rating. Login to rate!

Just a little fix

1
2
3
4
5
6
7
8
9
10
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(" ");
}
918aabb05e77cfa8e40d2a76a5168326

travis

October 9, 2007, October 09, 2007 14:51, permalink

1 rating. Login to rate!

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

CSS

1
2
3
4
5
#someSelector
{
	text-transform: capitalize;
}
Avatar

Emmett

October 9, 2007, October 09, 2007 15:51, permalink

2 ratings. Login to rate!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
//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();
  });
}

Your refactoring





Format Copy from initial code

or Cancel