I have function
firstLetterUppercase(str: string) {
let splitStr = str.toLowerCase().split(' ');
for (let i = 0; i < splitStr.length; i++) {
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
return splitStr.join(' ');
}
It’s work fine for string: Warszawa, Krakow, Bielsko – Biala etc.
I need change it to string ‘Bielsko-biala’.
(i need first char after ‘-‘ uppercase too).
How can i make it?
Please help me
4
Answers
You can use a regular expression and the replace() method to find and replace the lowercase letters after a dash with their uppercase counterparts.
Here is the updated code that will work for ‘Bielsko-biala’.
In the modified function, I have added a conditional statement to check if the current word contains a hyphen (‘-‘). If it does, I locate the index of the hyphen using indexOf(‘-‘). Then, it capitalizes the letter immediately after the hyphen by concatenating the relevant portions of the string with their capitalized counterparts.
If you want to transform your text into uppercase for every first letter of a word you can map your input after turning it into an array like this:
Then just call it with some params:
toUpperCase("first-asdasd")
Dont forget to return string and not the splited array
One way of doing it can be: