docs/reference/string/capitalize.md
Converts the first character of a string to uppercase and the remaining characters to lowercase.
const result = capitalize(str);
capitalize(str)Use capitalize when you want to make only the first letter uppercase and convert the rest to lowercase. It's useful for normalizing names or titles.
import { capitalize } from 'es-toolkit/string';
// Basic usage
capitalize('hello'); // returns 'Hello'
capitalize('WORLD'); // returns 'World'
capitalize('javaScript'); // returns 'Javascript'
It also handles empty strings and single character strings correctly.
import { capitalize } from 'es-toolkit/string';
capitalize(''); // returns ''
capitalize('a'); // returns 'A'
capitalize('A'); // returns 'A'
You can use it to normalize user input or create titles.
import { capitalize } from 'es-toolkit/string';
// Normalize user names
const userName = 'john DOE';
const formattedName = userName.split(' ').map(capitalize).join(' ');
// returns 'John Doe'
// Create titles
const title = capitalize('welcome to our website');
// returns 'Welcome to our website'
str (string): The string to capitalize the first character.(string): Returns a new string with the first character capitalized and the rest in lowercase.