Nodejs String Capitalize capitalize()

Here you can find the source of capitalize()

Method Source Code

/**//from w  w w  .  j  a va2 s  .  c o  m
 * Capitalize the first letter of the string.
 * @returns {string}
 */
String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.substr(1).toLowerCase();
};

Related

  1. capitalize()
    String.prototype.capitalize = function () {
      const charArray = this.toLowerCase().split('')
      charArray[0] = charArray[0].toUpperCase()
      return charArray.join('')
    
  2. capitalize()
    String.prototype.capitalize = String.prototype.capitalize || function(){
      var split = this.split(' ').filter(Boolean);
      var len = split.length;
      var capitalized_string = '';
      for (var i=0;i<len;i++) {
        capitalized_string = capitalized_string + split[i].charAt(0).toUpperCase() + split[i].slice(1).toLowerCase() + " ";
      return capitalized_string.slice(0, capitalized_string.length-1);
    
  3. capitalize()
    String.prototype.capitalize = function () {
        return this.toLowerCase().replace(/(^|\s)([a-z])/g, function (m, p1, p2) {
            return p1 + p2.toUpperCase();
        });
    }; 
    
  4. capitalize()
    String.prototype.capitalize = function() {
      var newString = "";
      for (var i = 0; i < this.length; i++) {
        if (i === 0 || this.charAt(i - 1) === " ") {
          newString += this.charAt(i).toUpperCase();
        } else {
          newString += this.charAt(i);
      return newString;
    };
    
  5. capitalize()
    String.prototype.capitalize = function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  6. capitalize()
    String.prototype.capitalize = function () {
        var str = this.toLowerCase();
        return str.charAt(0).toUpperCase() + str.substr(1);
    };
    
  7. capitalize() // eslint-disable-line
    String.prototype.capitalize = function() { 
      return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  8. capitalize(all)
    String.prototype.capitalize = function (all) {
      return this.split(/\s+/).map(function(word, i){
        return (i === 0 || all)
          ? word.charAt(0).uppercase() + word.drop(1)
          : word
      }).join(' ')
    };
    
  9. capitalize(lower)
    'use strict';
    String.prototype.capitalize = function(lower) {
        return (lower ? this.toLowerCase() : this).replace(
            /(?:^|\s)\S/g,
            function(a) { return a.toUpperCase(); });
    };