Javascript String titleize()

Description

Javascript String titleize()


String.prototype.titleize = function () {
  return this.substr(0,1).toUpperCase() + this.substr(1).toLowerCase()
}

Javascript String titleize()

String.prototype.titleize = function(){
  if(this.length == 0) return this;
  return this[0].toUpperCase() + this.substr(1).toLowerCase();
}

Javascript String titleize()

String.prototype.titleize = function() {
  var words = this.split(' ')
  var array = []/*w w w.j a  v a2s  .  c  om*/
  for (var i=0; i<words.length; ++i) {
    array.push(words[i].charAt(0).toUpperCase() + words[i].toLowerCase().slice(1))
  }
  return array.join(' ')
}

Javascript String titleize()

String.prototype.titleize = function() {
  var out = this;
  out = out.replace(/^\s*/, "");  // strip leading spaces
  out = out.replace(/_/g, ' ');
  out = out.replace(/^[a-z]|[^\s][A-Z]/g, function(str, offset) {
    if (offset === 0) {
      return(str.toUpperCase());
    } else {/*from  ww w . jav a2  s.c o m*/
      return(str.substr(0,1) + " " + str.substr(1).toUpperCase());
    }
  });
  return out;
};

Javascript String titleize()

String.prototype.titleize = function() {
    var string_array = this.split(' ');
    string_array = string_array.map(function(str) {
       return str.capitalize();
    });/*from   ww  w  .j  av  a2  s  . c  o  m*/

    return string_array.join(' ');
}

Javascript String titleize()

// Monkey Patch String.titleize
String.prototype.titleize = function () {
    return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};

Javascript String titleize()

String.prototype.titleize = function() {
  words = this.split(' ');
  
  for(i in words)
    words[i] = words[i].capitalize();/*  w ww.  j a va 2  s .c  o  m*/

  return words.join(' ');
}



PreviousNext

Related