toTitleCase
Converts a string into Title Case format, where the first letter of each word is capitalized.> toTitleCase(str: string): string
- str (string): The input string to be converted into Title Case. It can include mixed cases, numbers, or delimiters (e.g., spaces, dashes, underscores).
- Returns: A string formatted in Title Case, with each word capitalized and separated by a single space.
Example
import { toTitleCase } from "string-toolset";
// Convert a regular phrase to Title Case
console.log(toTitleCase("hello world")); // "Hello World"
// Handle uppercase strings with delimiters
console.log(toTitleCase("HELLO-WORLD")); // "Hello World"
// Convert snake_case to Title Case
console.log(toTitleCase("user_id")); // "User Id"
// Handle kebab-case
console.log(toTitleCase("to-title-case")); // "To Title Case"
// Handle numbers in the string
console.log(toTitleCase("chapter 1 of the book")); // "Chapter 1 Of The Book"
// Handle empty input
console.log(toTitleCase("")); // ""
Use Cases
- Formatting strings for UI headers or titles.
- Creating human-readable outputs from code or user inputs.
- Standardizing text for display in documents or applications.