Write a function that converts a given string into Title Case. In Title Case, the first letter of each word is capitalized, and the rest of the letters in each word are in lowercase.
Input: str = "Hey Aman is a correct way to convert a string to title case"
Output: "Hey Aman Is A Correct Way To Convert A String To Title Case"
function convertToTitleCaseUsingReplace(str) {
return str.replace(/\b\w/g, (char) => char.toUpperCase());
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingReplace(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case
function convertToTitleCaseUsingSplitMapJoin(str) {
return str
.split(" ")
.map(word => word[0].toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingSplitMapJoin(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case
function convertToTitleCaseUsingForLoop(str) {
const words = str.split(" ");
let titleCasedString = "";
for (let i = 0; i < words.length; i++) {
const word = words[i];
titleCasedString += (i > 0 ? " " : "") + word[0].toUpperCase() + word.slice(1).toLowerCase();
}
return titleCasedString;
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingForLoop(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case
function convertToTitleCaseUsingSplitMapJoin2(str) {
return str
.split(" ")
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingSplitMapJoin2(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case
function convertToTitleCaseUsingReduce(str) {
return str
.split(" ")
.reduce((titleCasedString, word) => {
return titleCasedString + (titleCasedString ? " " : "") + word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}, "");
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingReduce(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case
function convertToTitleCaseUsingArrayFromMap(str) {
return Array.from(str.split(" "))
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingArrayFromMap(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case
function convertToTitleCaseUsingMap(str) {
return str
.split(" ")
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingMap(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case
function convertToTitleCaseUsingRegexReplace(str) {
return str.replace(
/\b\w+/g,
(word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
);
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingRegexReplace(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case
function convertToTitleCaseUsingMatchReplace(str) {
return str
.toLowerCase()
.replace(/\b\w/g, (char) => char.toUpperCase());
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingMatchReplace(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case
function convertToTitleCaseUsingSplitArrayJoin(str) {
const words = str.split(" ");
return words
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
}
const str1 = "Hey Aman is a correct way to convert a string to title case";
convertToTitleCaseUsingSplitArrayJoin(str1); //output: Hey Aman Is A Correct Way To Convert A String To Title Case