Capitalize All

PreviousNext

Capitalize the first letter of every word in a string.

Docs
utilcnlib

Preview

Loading preview…
registry/default/strings/capitalize-all.ts
/**
 * Capitalizes the first letter of every word in a string.
 *
 * @param str - The input string.
 * @returns The title-cased string.
 *
 * @example
 * capitalizeAll("hello world"); // "Hello World"
 */
export function capitalizeAll(str: string): string {
  return str.replace(
    /(^|\s)(\S)/g,
    (_, space, char) => space + char.toUpperCase(),
  );
}

Installation

npx shadcn@latest add @utilcn/capitalize-all

Usage

import { CapitalizeAll } from "@/lib/capitalize-all"
CapitalizeAll()