String Utilities
capitalize
Capitalizes the first character of a string
Usage
import { capitalize } from '@/lib/capitalize';
const result = capitalize("typescript");
console.log(result); // "Typescript"Installation
pnpm dlx shadcn@latest add https://utilcn.dev/r/capitalize.jsonbunx --bun shadcn@latest add https://utilcn.dev/r/capitalize.jsonnpx shadcn@latest add https://utilcn.dev/r/capitalize.jsonyarn shadcn@latest add https://utilcn.dev/r/capitalize.jsonParameters
| Parameter | Type | Description |
|---|---|---|
str | string | The input string |
Returns
string - The string with the first character uppercased.
Examples
capitalize("typescript"); // "Typescript"
capitalize("hello world"); // "Hello world"
capitalize(""); // ""
capitalize("a"); // "A"Implementation
export function capitalize(str: string): string {
if (!str) return '';
return str.charAt(0).toUpperCase() + str.slice(1);
}