How do I make the first letter of a string uppercase, but not change the case of any of the other letters?
For example:
- this is a test -> This is a test
- the Eiffel Tower -> The Eiffel Tower
- /index.html -> /index.html
|
How do I make the first letter of a string uppercase, but not change the case of any of the other letters? For example:
|
||||
|
Another solution:
You could also add it to the
and use it like this:
|
|||||||||||||||||||||
|
|
A more object-oriented approach:
And then:
|
|||||||||||||||||||||
|
|
In CSS:
|
|||||||||||||||||||||
|
|
Here is a shortened version of the popular answer that gets the first letter by treating the string as an array:
Update: According to the comments below this doesn't work in IE 7 or below. Update 2: To avoid
|
|||||||||||||||||||||
|
If you're interested in the performance of a few different methods posted:Here are the fastest methods based on this jsperf test (ordered from fastest to slowest). As you can see, the first two methods are essentially comparable in terms of performance, whereas altering the
|
|||||||||
|
|
For another case I need it to capitalize the first letter and lowercase the rest. The following cases made me change this function:
|
|||||||||||||||||
|
|
Capitalize the first letter of all words in a string:
|
|||||||||||||||||
|
|
||||
|
|
|
We could get the first character with one of my favorite
And for all coffee-junkies:
...and for all guys who think that there's a better way of doing this, without extending native prototypes:
|
|||||||||
|
|
If you use underscore.js or Lo-Dash, the underscore.string library provides string extensions, including capitalize:
Example:
|
|||||||||||||
|
|
It seems to be easier in CSS:
This is from CSS text-transform Property (at W3Schools). |
|||||||||||||||||||||
|
|
|||||||||
|
|
If you are wanting to reformat all-caps text, you might want to modify the other examples as such:
This will ensure that the following text is changed:
|
||||
And then:
|
||||
|
|
Usage:
This is a text string => This Is A Text String |
|||||||||||||||||||||
|
|
||||
|
|
|
Checkout this solution:
|
|||||||||||||
|
(You may encapsulate it in a function or even add it to the String prototype if you use it frequently.) |
|||||
|
|
Here is a function called ucfirst() (short for "upper case first letter"):
You can capitalise a string by calling ucfirst("some string") -- for example,
It works by splitting the string into two pieces. On the first line it pulls out firstLetter and then on the second line it capitalises firstLetter by calling firstLetter.toUpperCase() and joins it with the rest of the string, which is found by calling str.substr(1). You might think this would fail for an empty string, and indeed in a language like C you would have to cater for this. However in JavaScript, when you take a substring of an empty string, you just get an empty string back. |
|||||||||||||||||
|
|
|||||
|
|
The
Thanks J-P for the aclaration. |
|||||||||||||||||||||
|
|
In CoffeeScript, add to the prototype for a string:
Usage would be:
Which yields:
|
|||||||||||||||||||||
|
|
If you're already (or considering) using
See their docs: https://lodash.com/docs#capitalize
https://lodash.com/docs/4.15.0#camelCase
|
|||||||||||||||||||||
|
Usage:
|
|||||
|
|
CoffeeScript
As String prototype method:
|
|||||
|
|
If you go with one of the regex answers, remember they will only work with ASCII characters. All your unicode letters will not be uppercased. The XRegExp library and its unicode plugins solve this problem if you want to stick with regexps. So something like this would work:
Considering that it still doesn't cover all possibilities (combined characters, see http://www.regular-expressions.info/unicode.html) it seems easier to just use the .charAt(0).toUpperCase() approach. |
|||
|
|
it will return |
|||||||||
|
|
Posting an edit of @salim's answer to include locale letter transformation.
|
|||
|
|
|
One possible solution:
Use this:
Here is working JS Fiddle |
||||
|
|
|
Or you could use Sugar.js capitalize() Example:
|
||||
|
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
return str.replace(/(\b\w)/gi,function(m){return m.toUpperCase();});– Muhammad Umer Nov 21 '14 at 19:25string[0].toUpperCase() + string.substring(1)– dr.dimitru Nov 25 '15 at 4:00