How do I make the first letter of a string uppercase in JavaScript?

前端 未结 30 2277
南方客
南方客 2020-11-21 05:00

How do I make the first letter of a string uppercase, but not change the case of any of the other letters?

For example:

  • \"this is a test\"
相关标签:
30条回答
  • 2020-11-21 05:36

    Here is a function called ucfirst() (short for "upper case first letter"):

    function ucfirst(str) {
        var firstLetter = str.substr(0, 1);
        return firstLetter.toUpperCase() + str.substr(1);
    }
    

    You can capitalise a string by calling ucfirst("some string") -- for example,

    ucfirst("this is a test") --> "This is a test"
    

    It works by splitting the string into two pieces. On the first line it pulls out firstLetter and then on the second line it capitalises firstLetter by calling firstLetter.toUpperCase() and joins it with the rest of the string, which is found by calling str.substr(1).

    You might think this would fail for an empty string, and indeed in a language like C you would have to cater for this. However in JavaScript, when you take a substring of an empty string, you just get an empty string back.

    0 讨论(0)
  • 2020-11-21 05:38
    String.prototype.capitalize = function(){
        return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase();
        } );
    };
    

    Usage:

    capitalizedString = someString.capitalize();
    

    This is a text string => This Is A Text String

    0 讨论(0)
  • 2020-11-21 05:39

    This is the 2018 ECMAScript 6+ Solution:

    const str = 'the Eiffel Tower';
    const newStr = `${str[0].toUpperCase()}${str.slice(1)}`;
    console.log('Original String:', str); // the Eiffel Tower
    console.log('New String:', newStr); // The Eiffel Tower

    0 讨论(0)
  • 2020-11-21 05:39

    I didn’t see any mention in the existing answers of issues related to astral plane code points or internationalization. “Uppercase” doesn’t mean the same thing in every language using a given script.

    Initially I didn’t see any answers addressing issues related to astral plane code points. There is one, but it’s a bit buried (like this one will be, I guess!)


    Most of the proposed functions look like this:

    function capitalizeFirstLetter(str) {
      return str[0].toUpperCase() + str.slice(1);
    }
    

    However, some cased characters fall outside the BMP (basic multilingual plane, code points U+0 to U+FFFF). For example take this Deseret text:

    capitalizeFirstLetter("                                                                    
    0 讨论(0)
  • 2020-11-21 05:39

    If you are wanting to reformat all-caps text, you might want to modify the other examples as such:

    function capitalize (text) {
        return text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
    }
    

    This will ensure that the following text is changed:

    TEST => Test
    This Is A TeST => This is a test
    
    0 讨论(0)
  • 2020-11-21 05:39
    var str = "test string";
    str = str.substring(0,1).toUpperCase() + str.substring(1);
    
    0 讨论(0)
提交回复
热议问题