Dave's Brain

Browse - programming tips - javascript title case

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;
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: