最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - string[0].toUpperCase() not returning expected results? - Stack Overflow

programmeradmin5浏览0评论

I am trying to capitalize the first letter in each word in a string. My code is as follows:

function LetterCapitalize(str)
{
    var words=str.split(" ");
    var newArray=[];
    for(i=0; i<words.length; i++)
    {
        var aWord=words[i];
        //this line below doesn't work...don't know why!
        aWord[0]=aWord[0].toUpperCase();
        newArray.push(aWord);
    };
    newArray.join(" ");
    return newArray;
};

So the issue I'm seeing is that when I try to assign aWord[0] to its uppercase value, the assignment doesn't happen? How do I modify this so that the assignment happens?

PS. I know there's a regular expression method that can do this in one fell swoop, but I just picked up javascript a few days ago and haven't gotten that far yet! as such, any tips that don't involve regexp are greatly appreciated

I am trying to capitalize the first letter in each word in a string. My code is as follows:

function LetterCapitalize(str)
{
    var words=str.split(" ");
    var newArray=[];
    for(i=0; i<words.length; i++)
    {
        var aWord=words[i];
        //this line below doesn't work...don't know why!
        aWord[0]=aWord[0].toUpperCase();
        newArray.push(aWord);
    };
    newArray.join(" ");
    return newArray;
};

So the issue I'm seeing is that when I try to assign aWord[0] to its uppercase value, the assignment doesn't happen? How do I modify this so that the assignment happens?

PS. I know there's a regular expression method that can do this in one fell swoop, but I just picked up javascript a few days ago and haven't gotten that far yet! as such, any tips that don't involve regexp are greatly appreciated

Share Improve this question asked Oct 15, 2015 at 8:20 YOO629YOO629 891 silver badge12 bronze badges 3
  • 3 You are only allowed to read a character, not mutate it. – zerkms Commented Oct 15, 2015 at 8:23
  • 3 I was going to answer this, but there are many good answers already. One further thing to note: you will probably want to return newArray.join(" ");, right now you're returning the array itself, not a string. – EelkeSpaak Commented Oct 15, 2015 at 8:24
  • @EelkeSpaak thanks for heads up! – YOO629 Commented Oct 15, 2015 at 8:41
Add a ment  | 

3 Answers 3

Reset to default 10

Strings are immutable, so this won't do anything:

aWord[0]=aWord[0].toUpperCase();

Instead, you have to create a new string:

aWord = aWord.substring(0, 1).toUpperCase() + aWord.substring(1);
// or:
aWord = aWord.charAt(0).toUpperCase() + aWord.substring(1);
// or on *most* engines (all modern ones):
aWord = aWord[0].toUpperCase() + aWord.substring(1);

aWord[0] is a char, and you cannot assign a char a new char, like 'a' = 'b'

Use a variable instead

for(i=0; i<words.length; i++)
{
    var aWord=words[i];
    //this line below doesn't work...don't know why!
    var newChar =aWord[0].toUpperCase();
    newArray.push(newChar);
};

you can write up a function that does the job for u,as below

function capsFirstLetter(str){
return str.substr(0,1).toUpperCase()+str.substr(1);

}
capsFirstLetter("hello") // Hello

or you can add the method in the String class and using it for all the string instances

   String.prototype.capsFirstLetter = function(){
       return this.substr(0,1).toUpperCase() + this.substr(1);
    } 
 "hello".capsFirstLetter() // "Hello"
发布评论

评论列表(0)

  1. 暂无评论