Dave's Brain

Browse - Programming Tips - How do make a string title case (first letter uppercase) in javaScript?

Date: 2009jun30
Language: javaScript

Q.  How do make a string title case (first letter uppercase) in javaScript?

A.

function ucFirst(a) { return a.substr(0,1).toUpperCase() + a.substr(1); } function titleCaseWord(a) { return a.substr(0,1).toUpperCase() + a.substr(1).toLowerCase(); } function titleCase(s) { var title = '', a, word; a = s.split(' '); for (i = 0; i < a.length; i++) { if (title != '') title += ' '; title += titleCaseWord(a[i]); } return title; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.