12

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");

5
  • you mean first character of every word of a string? Commented Dec 11, 2015 at 6:27
  • 2
    Please find you answer here Capitalize the first letter of string in JavaScript Commented Dec 11, 2015 at 6:28
  • Is I'm A Little Tea Pot expected output Commented Dec 11, 2015 at 6:34
  • @Tushar I guess so...has a valid answer from gurvinder372 Commented Dec 11, 2015 at 8: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(); }); Commented Dec 11, 2015 at 8:38

5 Answers 5

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") );
Sign up to request clarification or add additional context in comments.

1 Comment

this is the correct answer...
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

Comments

2
    function capitalise(string) {
        return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
    }
   capitalise("smallletters") ;// Smallletters

Comments

1

You could simply do:

function capitalFirst(str) {
    return str.charAt(0).toUpperCase() + str.slice(1);
}

Comments

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(" ");
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.