Convert String to Title Case

medium

By - Aman Pareek

Last Updated - 31/08/2024

Problem Statement

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.

Example 1

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"

Solution 1: Using replace Method with Regex

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 

Solution 2: Using split, map, splice, and join

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 

Solution 3: Using for Loop

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 

Solution 4: Using split, map, and join

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 

Solution 5: Using reduce

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 

Solution 6: Using Array.from and map

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 

Solution 7: Using Array.prototype.map

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 

Solution 8: Using Regular Expression with replace

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 

Solution 9: Using String.prototype.replace with Match

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 

Solution 10: Using String.prototype.split, Array.from, and join

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 

Popular Solutions