1//capitalize only the first letter of the string.
2function capitalizeFirstLetter(string) {
3 return string.charAt(0).toUpperCase() + string.slice(1);
4}
5//capitalize all words of a string.
6function capitalizeWords(string) {
7 return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
8};
1const lower = 'this is an entirely lowercase string';
2const upper = lower.charAt(0).toUpperCase() + lower.substring(1);
1// for lowercase on first letter
2let s = "This is my string"
3let lowerCaseFirst = s.charAt(0).toLowerCase() + s.slice(1)
1function capitalizeFirstLetter(name) {
2 return name.replace(/^./, name[0].toUpperCase())
3}
1function titleCase(str) {
2 var splitStr = str.toLowerCase().split(' ');
3 for (var i = 0; i < splitStr.length; i++) {
4 // You do not need to check if i is larger than splitStr length, as your for does that for you
5 // Assign it back to the array
6 splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
7 }
8 // Directly return the joined string
9 return splitStr.join(' ');
10}
11
12document.write(titleCase("I'm a little tea pot"));
1function capitalizeFirstLetter(str, inAllWordsOfString = false) {
2 if (!inAllWordsOfString) {
3 //convert given string to lowercase
4 let lowerStr = str.toLowerCase();
5 // Now convert first character to upper case
6 let firstCharacter = str.charAt(0).toUpperCase();
7 // Now combine firstCharacter and lowerStr and return
8 return firstCharacter + lowerStr.slice(1);
9 } else {
10 let str1 = str.split(" ");
11 let returnStr = "";
12
13 for (let i = 0; i < str1.length; i++) {
14 let lowerStr = str1[i].toLowerCase();
15 returnStr =
16 returnStr + str1[i].charAt(0).toUpperCase() + lowerStr.slice(1) + " ";
17 }
18 return returnStr.trim();
19 }
20}
21
22capitalizeFirstLetter('hello word');
23//output: Hello word
24
25capitalizeFirstLetter('hello word', true);
26//output : Hello Word