How to Upper Case Every First Letter of Word in a String

How can I capitalize the first letter of each word in a string using JavaScript?

You are not assigning your changes to the array again, so all your efforts are in vain. Try this:

function titleCase(str) {   var splitStr = str.toLowerCase().split(' ');   for (var i = 0; i < splitStr.length; i++) {       // You do not need to check if i is larger than splitStr length, as your for does that for you       // Assign it back to the array       splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);        }   // Directly return the joined string   return splitStr.join(' '); }
document.write(titleCase("I'm a little tea pot"));

How to upper case every first letter of word in a string?

Have a look at ACL WordUtils.

WordUtils.capitalize("your string") == "Your String"

How to capitalize the first letter of word in a string using Java?

If you only want to capitalize the first letter of a string named input and leave the rest alone:

String output = input.substring(0, 1).toUpperCase() + input.substring(1);

Now output will have what you want. Check that your input is at least one character long before using this, otherwise you'll get an exception.

How to capitalize first letter of each word, like a 2-word city?

There's a good answer here:

function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();

or in ES6:

var text = "foo bar loo zoo moo";
text = text.toLowerCase()
.split(' ')
.map((s) => s.charAt(0).toUpperCase() + s.substring(1))
.join(' ');

How to capitalize the first character of each word in a string

WordUtils.capitalize(str) (from apache commons-text)

(Note: if you need "fOO BAr" to become "Foo Bar", then use capitalizeFully(..) instead)

Make first letter of words uppercase in a string

There is a function in the built-in strings package called Title.

s := "INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"

How can I capitalize the first letter of each word in a string?

The .title() method of a string (either ASCII or Unicode is fine) does this:

>>> "hello world".title()
'Hello World'
>>> u"hello world".title()
u'Hello World'

However, look out for strings with embedded apostrophes, as noted in the docs.

The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:

>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"

How to capitalise the first letter of every word in a string

This snippet might help:

function firstLetterUpperCase(strData:String):String 
var strArray:Array = strData.split(' ');
var newArray:Array = [];
for (var str:String in strArray)
newArray.push(strArray[str].charAt(0).toUpperCase() + strArray[str].slice(1));
return newArray.join(' ');

var strs = "Testing cases (e.g. o'Neil) and others (e.g. connah's quay)."

Result is:

//Testing Cases (e.g. O'Neil) And Others (e.g. Connah's Quay).

Related Topics

Leave a reply