Javascript String titleCase()

Description

Javascript String titleCase()


String.prototype.titleCase = function () {  // eslint-disable-line no-extend-native
  return this.replace(/\w\S*/g, (txt) => {
    return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  });/*from  ww w  . j av  a2s  . c o  m*/
};

Javascript String titleCase()

'use strict'/*from  w  w w  .j a v a  2  s .  c  o m*/

function titleCase(str) {
    var i = 0,
        titledString,
        tc,
        rw,
        stagingArray = str.split(' ');

    stagingArray.forEach(function(word) {
        tc = word.substring(0,1).toUpperCase();
        rw = word.substring(1).toLowerCase();
        titledString = tc + rw;
        stagingArray.splice(i,1,titledString);
        i++;
       }); 

    titledString = stagingArray.join(' ');
    return titledString;
};
        
String.prototype.titleCase = String.prototype.titleCase || titleCase;

Javascript String titleCase()

String.prototype.titleCase = function() {
  return this.toLowerCase().split(' ').map(function(word) {
    return (word.charAt(0).toUpperCase() + word.slice(1));
  }).join(' ');/*from  www. ja  va2s  . com*/
}



PreviousNext

Related