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

前端 未结 30 2200
南方客
南方客 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:16

    If you're interested in the performance of a few different methods posted:

    Here are the fastest methods based on this jsperf test (ordered from fastest to slowest).

    As you can see, the first two methods are essentially comparable in terms of performance, whereas altering the String.prototype is by far the slowest in terms of performance.

    // 10,889,187 operations/sec
    function capitalizeFirstLetter(string) {
        return string[0].toUpperCase() + string.slice(1);
    }
    
    // 10,875,535 operations/sec
    function capitalizeFirstLetter(string) {
        return string.charAt(0).toUpperCase() + string.slice(1);
    }
    
    // 4,632,536 operations/sec
    function capitalizeFirstLetter(string) {
        return string.replace(/^./, string[0].toUpperCase());
    }
    
    // 1,977,828 operations/sec
    String.prototype.capitalizeFirstLetter = function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    }
    

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

    SHORTEST 3 solutions, 1 and 2 handle cases when s string is "", null and undefined:

     s&&s[0].toUpperCase()+s.slice(1)        // 32 char
    
     s&&s.replace(/./,s[0].toUpperCase())    // 36 char - using regexp
    
    'foo'.replace(/./,x=>x.toUpperCase())    // 31 char - direct on string, ES6
    

    let s='foo bar';
    
    console.log( s&&s[0].toUpperCase()+s.slice(1) );
    
    console.log( s&&s.replace(/./,s[0].toUpperCase()) );
    
    console.log( 'foo bar'.replace(/./,x=>x.toUpperCase()) );

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

    In CSS:

    p:first-letter {
        text-transform:capitalize;
    }
    
    0 讨论(0)
  • 2020-11-21 05:19

    Capitalize the first letter of all words in a string:

    function ucFirstAllWords( str )
    {
        var pieces = str.split(" ");
        for ( var i = 0; i < pieces.length; i++ )
        {
            var j = pieces[i].charAt(0).toUpperCase();
            pieces[i] = j + pieces[i].substr(1);
        }
        return pieces.join(" ");
    }
    
    0 讨论(0)
  • 2020-11-21 05:19

    There is a very simple way to implement it by replace. For ECMAScript 6:

    'foo'.replace(/^./, str => str.toUpperCase())
    

    Result:

    'Foo'
    
    0 讨论(0)
  • 2020-11-21 05:19

    It's always better to handle these kinds of stuff using CSS first, in general, if you can solve something using CSS, go for that first, then try JavaScript to solve your problems, so in this case try using :first-letter in CSS and apply text-transform:capitalize;

    So try creating a class for that, so you can use it globally, for example: .first-letter-uppercase and add something like below in your CSS:

    .first-letter-uppercase:first-letter {
        text-transform:capitalize;
    }
    

    Also the alternative option is JavaScript, so the best gonna be something like this:

    function capitalizeTxt(txt) {
      return txt.charAt(0).toUpperCase() + txt.slice(1); //or if you want lowercase the rest txt.slice(1).toLowerCase();
    }
    

    and call it like:

    capitalizeTxt('this is a test'); // return 'This is a test'
    capitalizeTxt('the Eiffel Tower'); // return 'The Eiffel Tower'
    capitalizeTxt('/index.html');  // return '/index.html'
    capitalizeTxt('alireza');  // return 'Alireza'
    

    If you want to reuse it over and over, it's better attach it to javascript native String, so something like below:

    String.prototype.capitalizeTxt = String.prototype.capitalizeTxt || function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    }
    

    and call it as below:

    'this is a test'.capitalizeTxt(); // return 'This is a test'
    'the Eiffel Tower'.capitalizeTxt(); // return 'The Eiffel Tower'
    '/index.html'.capitalizeTxt();  // return '/index.html'
    'alireza'.capitalizeTxt();  // return 'Alireza'
    
    0 讨论(0)
提交回复
热议问题