Nodejs String to Upper Case ucwords()

Here you can find the source of ucwords()

Method Source Code

/**//w w  w  .  j  a  v a2s  .co  m
@Name: String.prototype.ucwords
@Author: Paul Visco
@Version: 1.0 11/19/07
@Description: Converts all first letters of words in a string to uppercase.  Great for titles.
@Return: String The original string with all first letters of words converted to uppercase.
@Example:
var myString = 'hello world';

var newString = myString.ucwords();
//newString = 'Hello World'
*/

String.prototype.ucwords = function(){
   var arr = this.split(' ');
   
   var str ='';
   arr.forEach(function(v){
      str += v.charAt(0).toUpperCase()+v.slice(1,v.length)+' ';
   });
   return str;
};

Related

  1. toUpperLowerCase()
    String.prototype.toUpperLowerCase = function() {
        var string = this.split("");
        string[0] = string[0].toUpperCase();
        return string.join("");
    };
    String.prototype.firstUpper = function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  2. ucWords()
    String.prototype.ucWords = function () {
        return this.split(' ').map(function (w) {
            return w.substr(0, 1).toUpperCase() + w.substr(1); 
        }).join(' ');
    
  3. uc_words()
    String.prototype.uc_words = function(){
        return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){return p1+p2.toUpperCase();});
    };
    
  4. ucwords()
    String.prototype.ucwords = function() {
        return (this + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
          return $1.toUpperCase();
      });
    
  5. ucwords()
    String.prototype.ucwords = function () {
      var words = this.split(' ');
      for (var i = 0; i < words.length; i++)
        words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
      return words.join(' ');
    };
    module.exports = function () {
    };
    
  6. ucwords()
    String.prototype.ucwords = function(){
      return this.replace(/(?:^|\s)\S/g,function(v){return v.toUpperCase();});
    };
    
  7. ucwords()
    String.prototype.ucwords = function () {
      return this.toLowerCase().replace(/^.|\s\S/g, function (a) {
        return a.toUpperCase();
      });
    };
    
  8. upWord()
    String.prototype.upWord = function (){
      var words = this.split(" ");
      var newWords = words.map(function(elem,index) {
        return (elem[0].toUpperCase() + elem.slice(1));
      })
      return newWords.join(" ");
    var str = "the quick fox jump over the lazy dog";
    console.log(str.upWord());
    ...
    
  9. upcase()
    String.prototype.upcase = function () {
      return this.valueOf().toUpperCase();
    };