Skip to content

capitalize

Capitalizes the first letter of a string. By default, lowercases the remaining characters. Pass { lowercaseRest: false } to only uppercase the first character.

Available since v1.9.0

import { capitalize } from '@helpers4/string';
capitalize(str: string, options?: CapitalizeOptions): string
ParameterTypeDescription
strstringThe string to capitalize
optionsCapitalizeOptionsOptions (optional)

string — String with first letter uppercased

Uppercases the first letter and lowercases the rest.

capitalize('hello')
// => 'Hello'

Lowercases all letters except the first one (default behaviour).

capitalize('hELLO')
// => 'Hello'

Uppercase first only — leave rest untouched

Section titled “Uppercase first only — leave rest untouched”

Use { lowercaseRest: false } to preserve the original casing of the remaining characters.

capitalize('hELLO', { lowercaseRest: false })
// => 'HELLO'

View source on GitHub