utilcn logoutilcn
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.json
bunx --bun shadcn@latest add https://utilcn.dev/r/capitalize.json
npx shadcn@latest add https://utilcn.dev/r/capitalize.json
yarn shadcn@latest add https://utilcn.dev/r/capitalize.json

Parameters

ParameterTypeDescription
strstringThe 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);
}