Capitalize first letter in string.

by sunil puvvada

JavaScript

// Capitalize First Letter of the String
String.prototype.capitalize = function () {
    return this.charAt(0).toUpperCase() + this.slice(1);
};
// Capitalize First Letter of the String
String.prototype.upperFirst = function () {    
    return (this.replace(/^(.)/g,
    function (c) {
        return c.toUpperCase();
    }));
};
// Capitalize First Letter of each word in sentence.
String.prototype.upperFirstAll = function () {    
    return (this.replace(/^(.)|(\s|\-)(.)/g,
    function (c) {
        return c.toUpperCase();
    }));
};

$(function () {
    var data = 'hello demon, how are you doin? ';
    $('body').html(data.upperFirstAll());
    $('body').append(data.upperFirst());
});
// reference: http://stackoverflow.com/questions/2017456/with-jquery-how-do-i-capitalize-the-first-letter-of-a-text-field-while-the-user