Javascript String toTitleCase()

Description

Javascript String toTitleCase()


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

Javascript String toTitleCase()

String.prototype.toTitleCase = function(){
    return this.toLowerCase().replace(/(^[a-z]| [a-z]|-[a-z])/g,
        function($1){
            return $1.toUpperCase();
        }// w  ww .j a  va2s  . co m
    );
};

Javascript String toTitleCase()

String.prototype.toTitleCase = function() {
  var str = this;
  return str.toLowerCase().split(' ').map(function(word) {
    return (word.charAt(0).toUpperCase() + word.slice(1));
  }).join(' ');/*from ww  w.jav a  2 s  . c o m*/
}

Javascript String toTitleCase()

String.prototype.toTitleCase = function() {
  const result = this.replace(/([A-Z])/g, " $1")
  return `${result.charAt(0).toUpperCase()}${result.slice(1)}`
}

Javascript String toTitleCase()

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

Javascript String toTitleCase()

String.prototype.toTitleCase = function () {
 var str = this;//  ww  w .  j a  v  a2 s .c om
 var newStr = '';

 var words = str.split(' ');
 for (var i = 0; i < words.length; i++) {
  newStr += ' ' + words[i].slice(0, 1).toUpperCase() + words[i].slice(1).toLowerCase();
 }

 return newStr.trim();
};

Javascript String toTitleCase()

let text = 'Get the best out of Node.js'
let lower = text.toLowerCase()
let upper = text.toUpperCase()

String.prototype.toTitleCase = function () {
  let arr = String.prototype.split.call(this, ' ')
  let arr2 = arr.map(str => `${str[0].toUpperCase()}${str.substr(1)}`)
  return Array.prototype.join.call(arr2, ' ')
}

let title = text.toTitleCase()

console.log(text)/*from   ww w  .  jav a2s . co  m*/
console.log(lower)
console.log(upper)
console.log(title)

Javascript String toTitleCase()

String.prototype.toTitleCase = function () {
  return this[0].toUpperCase() + this.substring(1).toLowerCase();
};

Javascript String toTitleCase()

String.prototype.toTitleCase = function toTitleCase() {
 return this.split(' ').map(a => a[0].toUpperCase() + a.slice(1)).join(' ');
};

Javascript String toTitleCase()

'use strict';//  w w w . j  a va2s .  com

var _ = require('underscore');

String.prototype.toTitleCase = function() {
  var exceptions = ['a', 'an', 'the', 'at', 'by', 'for', 'in', 'of', 'on', 'to', 'up', 'and', 'as', 'but', 'or', 'nor'];
  return _.map(this.split(' '), function(word) {
    return _.find(exceptions, function(compare) {
      return compare === word.toLowerCase();
    }) ? word : word.charAt(0).toUpperCase() + word.slice(1);
  }).join(' ');
};

Javascript String toTitleCase()

var smallword=["bin","binti","to","a","for","in"];


function is_in_small(text){
  var i=0;//from  w  w  w .  ja v  a2s  .  co m
  while(i<smallword.length){
    if(smallword[i]==text.toLowerCase()){
      return true;
    }
    i=i+1;
  }
  return false;
}

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

Javascript String toTitleCase()

/**/*w  w w .  j a  v a  2s  .c  o m*/
 * js-playing-cards
 *
 * Copyright ? 2016 Daryl Lukas. All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE.txt file in the root directory of this source tree.
 */

"use strict";

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

class Card {
    constructor(suit, rank) {
        this.suit = suit;
        this.rank = rank;
    }

    getSuit() {
        return this.suit;
    }

    getRank() {
        return this.rank;
    }

    toString() {
        return `${this.rank.toTitleCase()} of ${this.suit.toTitleCase()}`
    }
}

module.exports = Card;

Javascript String toTitleCase()

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

Javascript String toTitleCase()

String.prototype.toTitleCase = function(){
  var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;

  return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
    if (index > 0 && index + match.length !== title.length &&
      match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
      (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
      title.charAt(index - 1).search(/[^\s-]/) < 0) {
      return match.toLowerCase();
    }/*w  ww  .ja v a2  s  .  c  o  m*/

    if (match.substr(1).search(/[A-Z]|\../) > -1) {
      return match;
    }

    return match.charAt(0).toUpperCase() + match.substr(1);
  });
};

Javascript String toTitleCase()

String.prototype.toTitleCase = function () {
    var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;

    return this.replace(/([^\W_]+[^\s-]*) */g, function (match, p1, index, title) {
        if (index > 0 && index + p1.length !== title.length &&
            p1.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
            title.charAt(index - 1).search(/[^\s-]/) < 0) {
            return match.toLowerCase();
        }/*from  w ww.  j a va2s  .c  o m*/

        if (p1.substr(1).search(/[A-Z]|\../) > -1) {
            return match;
        }

        return match.charAt(0).toUpperCase() + match.substr(1);
    });
};

Javascript String toTitleCase()

String.prototype.toTitleCase = function () {
  if (!this) throw new Error('Null refference extension.');
  if (!this.length) return this;
  
  var words = this.split(' ');
  
  if (words.length === 1) 
    return this[0].toUpperCase() + this.substring(1).toLowerCase();

  for (var i = 0; i < words.length; i++) {
    words[i] = words[i].toTitleCase();/*  ww  w  . ja  v  a2s .co m*/
  };

  return words.join(' ');
};



PreviousNext

Related