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\"
p::first-letter {
text-transform: uppercase;
}
%a
, this selector would apply to %
and as such a
would not be capitalized.:first-letter
).Since there are numerous answers, but none in ES2015 that would solve original problem efficiently, I came up with the following:
const capitalizeFirstChar = str => str.charAt(0).toUpperCase() + str.substring(1);
parameters => function
is so called arrow function.capitalizeFirstChar
instead of capitalizeFirstLetter
, because OP didn't asked for code that capitalizes the first letter in the entire string, but the very first char (if it's letter, of course).const
gives us the ability to declare capitalizeFirstChar
as constant, which is desired since as a programmer you should always explicitly state your intentions.string.charAt(0)
and string[0]
. Note however, that string[0]
would be undefined
for empty string, so it should be rewritten to string && string[0]
, which is way too verbose, compared to the alternative.string.substring(1)
is faster than string.slice(1)
.