utilcn logoutilcn
String Utilities

capitalizeAll

Capitalizes the first letter of every word in a string

Usage

import { capitalizeAll } from '@/lib/capitalize-all';

const result = capitalizeAll("hello world");
console.log(result); // "Hello World"

Installation

pnpm dlx shadcn@latest add https://utilcn.dev/r/capitalize-all.json
bunx --bun shadcn@latest add https://utilcn.dev/r/capitalize-all.json
npx shadcn@latest add https://utilcn.dev/r/capitalize-all.json
yarn shadcn@latest add https://utilcn.dev/r/capitalize-all.json

Parameters

ParameterTypeDescription
strstringThe input string

Returns

string - The title-cased string.

Examples

capitalizeAll("hello world"); // "Hello World"
capitalizeAll("the quick brown fox"); // "The Quick Brown Fox"
capitalizeAll("javascript is awesome"); // "Javascript Is Awesome"
capitalizeAll("multiple   spaces"); // "Multiple   Spaces"

Implementation

export function capitalizeAll(str: string): string {
  return str.replace(
    /(^|\s)(\S)/g,
    (_, space, char) => space + char.toUpperCase(),
  );
}