1.38.0capitalize
Converts the first character of a string to uppercase while leaving the rest of the string unchanged.
capitalize(value):stringBehavior
If the input string is empty, capitalize returns it unchanged. Otherwise, it converts the first character to uppercase and appends the rest of the original string unchanged.
Preserves the rest
capitalize does not lowercase or otherwise normalize the remaining characters. Only the first character is transformed.
Empty strings
An empty string is returned directly without modification.
Parameters
valuestringrequiredThe string whose first character should be capitalized.
Returns
stringThe string with its first character converted to uppercase.
Examples
Basic usage
Capitalize the first character of a lowercase string.
import { capitalize } from "@cyref/js";
capitalize("hello");
// "Hello"Mixed case
Only the first character is changed. The rest remains unchanged.
capitalize("hello WORLD");
// "Hello WORLD"Already capitalized
A string that already starts with an uppercase character remains unchanged.
capitalize("Hello");
// "Hello"Empty string
An empty string is returned unchanged.
capitalize("");
// ""Rest of the string
Characters after the first one are preserved exactly as they are.
capitalize("hELLO wORLD");
// "HELLO wORLD"Single character
A single character is converted to uppercase.
capitalize("a");
// "A"