Capitalize v1.0 //JavaScript Repository

Description

Capitalizes a string, first letter in upper case and the rest in lower case.
Created: 2005.11.20 - Modified: 2005.11.21

Code ( Download )

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/capitalize [v1.0]

String.prototype.capitalize = function(){
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase();
    });
};

Example ( See example )

<script type="text/javascript">
//<![CDATA[

var s = "x jonas Raoni SOARES silva ~84";
document.write(('"' + s + '".capitalize() = ').bold(), s.capitalize());

//]]>
</script>

Help

String.capitalize(void): String
Returns the string with the first letter of the words in upper case and the others in lower case.

Rank (10 votes)

4.30
0 1 2 3 4 5