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.jsonbunx --bun shadcn@latest add https://utilcn.dev/r/capitalize-all.jsonnpx shadcn@latest add https://utilcn.dev/r/capitalize-all.jsonyarn shadcn@latest add https://utilcn.dev/r/capitalize-all.jsonParameters
| Parameter | Type | Description |
|---|---|---|
str | string | The 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(),
);
}