Uppercase first letter of variable

后端 未结 23 1460
粉色の甜心
粉色の甜心 2020-11-30 20:55

I have searched over the web can can\'t find anything to help me. I want to make the first letter of each word upper case within a variable.

So far i have tried:

23条回答
  •  心在旅途
    2020-11-30 21:16

    Based completely on @Dementric 's answer, this solution is ready to call with a simple jQuery method, 'ucwords'... Thanks to everyone who contributed here!!!

    $.extend({
    ucwords : function(str) {
        strVal = '';
        str = str.split(' ');
        for (var chr = 0; chr < str.length; chr++) {
            strVal += str[chr].substring(0, 1).toUpperCase() + str[chr].substring(1, str[chr].length) + ' '
        }
        return strVal
    }
    

    });

    EXAMPLE: This can be called using the method

    var string = "this is a test";
    string = $.ucwords(string); // Returns "This Is A Test"
    

提交回复
热议问题