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)"
fmt.Println(strings.Title(strings.ToLower(s)))
https://go.dev/play/p/THsIzD3ZCF9
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(' ');
}
//testing
var strs = "Testing cases (e.g. o'Neil) and others (e.g. connah's quay)."
trace(firstLetterUpperCase(strs));
Result is:
//Testing Cases (e.g. O'Neil) And Others (e.g. Connah's Quay).
Related Topics
How to Use Sqoop in Java Program
Best Language to Parse Extremely Large Excel 2007 Files
How to Get Which Jradiobutton Is Selected from a Buttongroup
Keystore Type: Which One to Use
Spring Batch - Using an Itemwriter with List of Lists
A Rotated Square Panel in Java Gui
Why Doesn't My Compare Work Between Char and Int in Java
Is It Worth to Use Slf4J with Log4J2
Best Practice to Use Httpclient in Multithreaded Environment
How to Build a Docker Container for a Java Application
Java Bigdecimal: Round to the Nearest Whole Value
Java: Rationale of the Cloneable Interface
What's the Best Way to Implement 'Next' and 'Previous' on an Enum Type
Java 8 Streams - Collect VS Reduce
Constantly Update UI in Java Fx Worker Thread
09 Is Not Recognized Where as 9 Is Recognized
Abnormal Behaviour of the Zoom in and Zoom Out Functionality of the Jfreechart