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 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)
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 capitalize first character of each word in a string?
To capitalize all the words in a sentence, you can use re.sub
and re.findall
:
import re
def capitalize_string(s):
return re.sub('(?<=^)[a-z]|(?<=\s)[a-z]', '{}', s).format(*map(str.upper, re.findall('(?<=^)[a-z]|(?<=\s)[a-z]', s)))
strings = ['q w e r G H J K M', 'hello world lol', "1 w 2 r 3g"]
result = list(map(capitalize_string, strings))
Output:
['Q W E R G H J K M', 'Hello World Lol', '1 W 2 R 3g']
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 the first character of each word, or the first character of a whole string, with C#?
As discussed in the comments of @miguel's answer, you can use TextInfo.ToTitleCase
which has been available since .NET 1.1. Here is some code corresponding to your example:
string lipsum1 = "Lorem lipsum et";
// Creates a TextInfo based on the "en-US" culture.
TextInfo textInfo = new CultureInfo("en-US",false).TextInfo;
// Changes a string to titlecase.
Console.WriteLine("\"{0}\" to titlecase: {1}",
lipsum1,
textInfo.ToTitleCase( lipsum1 ));
// Will output: "Lorem lipsum et" to titlecase: Lorem Lipsum Et
It will ignore casing things that are all caps such as "LOREM LIPSUM ET" because it is taking care of cases if acronyms are in text so that "IEEE" (Institute of Electrical and Electronics Engineers) won't become "ieee" or "Ieee".
However if you only want to capitalize the first character you can do the solution that is over here… or you could just split the string and capitalize the first one in the list:
string lipsum2 = "Lorem Lipsum Et";
string lipsum2lower = textInfo.ToLower(lipsum2);
string[] lipsum2split = lipsum2lower.Split(' ');
bool first = true;
foreach (string s in lipsum2split)
{
if (first)
{
Console.Write("{0} ", textInfo.ToTitleCase(s));
first = false;
}
else
{
Console.Write("{0} ", s);
}
}
// Will output: Lorem lipsum et
Related Topics
Y Returns 2012 While Y Returns 2011 in Simpledateformat
Received Fatal Alert: Handshake_Failure Through Sslhandshakeexception
Should I Always Use a Parallel Stream When Possible
How to Set or Change the Default Java (Jdk) Version on Macos
How to Use Key Bindings Instead of Key Listeners
Are Fields Initialized Before Constructor Code Is Run in Java
Causes of Getting a Java.Lang.Verifyerror
How to Put a Control in the Jtableheader of a Jtable
How to Remove Objects from an Array in Java
What Is the 'Instanceof' Operator Used For in Java