11

I've looked all over for how to capitalize the first character of every word of a string, but nothing helped me. I need to set an entered string to a heading capital character lower case one . I've tried this:

function titleCase(str) {
//converting the giving string into array
  str =str.split(" "); 
//iterating over all elem.s in the array
  for(var i=0;i<str.length;i++){        
//converting each elem. into string
    str[i]=str[i].toString(); 
//converting the first char to upper case &concatenating to the rest chars
    str[i]=str[i].toUpperCase(str[i].charAt(0))+ str[i].substring(1);
  }
  return str;
}
titleCase("I'm a little tea pot");
benzkji
  • 1,718
  • 20
  • 41
  • you mean first character of every word of a string? – gurvinder372 Dec 11 '15 at 06:27
  • 2
    Please find you answer here [Capitalize the first letter of string in JavaScript](http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript) – Mûhámmàd Yäsår K Dec 11 '15 at 06:28
  • Is `I'm A Little Tea Pot` expected output – Tushar Dec 11 '15 at 06:34
  • @Tushar I guess so...has a valid answer from gurvinder372 – benzkji Dec 11 '15 at 08:14
  • @benzkji You can't see deleted answers, I also had answer `function titleCase(str) { str = str.split(" "); for (var i = 0; i < str.length; i++) { str[i] = str[i][0].toUpperCase() + str[i].substring(1); } return str.join(' '); } var str = titleCase("I'm a little tea pot");` **and** `var str = "i'm a little tea pot"; str = str.replace(/^[a-z]|\s[a-z]/g, function(m) { return m.toUpperCase(); });` – Tushar Dec 11 '15 at 08:38

5 Answers5

3

if you want to uppercase first character of every word in the string (looks like what you are doing with your code)

function titleCase(str) {
  str =str.split(" "); 
  for(var i=0;i<str.length;i++)
  {        
    str[i]=str[i].charAt(0).toUpperCase + str[i].substring(1);
  }
  return str.join(" ");
}
alert( titleCase("I'm a little tea pot") );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
2
function firstToUpperCase( str ) {
    return str.substr(0, 1).toUpperCase() + str.substr(1);
}

var str = 'hello, I\'m a string';
var uc_str = firstToUpperCase( str );

console.log( uc_str ); //Hello, I'm a string
Mandeep Singh
  • 1,287
  • 14
  • 34
2
    function capitalise(string) {
        return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
    }
   capitalise("smallletters") ;// Smallletters
Jijesh Cherayi
  • 1,111
  • 12
  • 15
1

You could simply do:

function capitalFirst(str) {
    return str.charAt(0).toUpperCase() + str.slice(1);
}
GMaiolo
  • 4,207
  • 1
  • 21
  • 37
1

Try something like this:

String.prototype.titleCase = function(){
    return this[0].toUpperCase() + this.slice(1)
}

Usage:

"hello my name is Jacques".titleCase();

If you want to capitalize the character at the beginning of each word, try something like this:

String.prototype.capitalize = function(){
    return this.split(" ")
               .map(function(){
                   return this[0].toUpperCase() + this.slice(1);
               }).join(" ");
}
Jacques Marais
  • 2,666
  • 14
  • 33