1.38.0capitalizeWords
Capitalizes the first character of every space-separated word and converts the remaining characters of each word to lowercase.
capitalizeWords(value):stringBehavior
The string is split using a single space character. Each resulting part is processed independently. Non-empty words have their first character converted to uppercase and the rest converted to lowercase. The processed parts are then joined using a single space.
Spacing
Because split and join both use a single space, leading, trailing, and consecutive spaces are preserved in the resulting string.
Casing rules
Every non-empty word starts with an uppercase character, while all remaining characters in that word are converted to lowercase.
Empty parts
When multiple spaces occur next to each other, split produces empty strings. These empty strings are returned unchanged, preserving the original spacing.
Parameters
valuestringrequiredThe string whose words should be capitalized.
Returns
stringA string with the first character of each space-separated word uppercase and the remaining characters lowercase.
Examples
Basic usage
Capitalize every word in a sentence.
import { capitalizeWords } from "@cyref/js";
capitalizeWords("hello world");
// "Hello World"Uppercase input
Each word is normalized with an uppercase first character and lowercase remaining characters.
capitalizeWords("HELLO WORLD");
// "Hello World"Mixed case
Mixed casing is normalized for every word.
capitalizeWords("hELLo WoRLD");
// "Hello World"Single word
A single word is capitalized and its remaining characters are lowercased.
capitalizeWords("hELLO");
// "Hello"Multiple spaces
Empty parts created by consecutive spaces are preserved.
capitalizeWords("hello world");
// "Hello World"Leading and trailing spaces
Leading and trailing spaces are preserved.
capitalizeWords(" hello world ");
// " Hello World "Empty string
An empty string is returned unchanged.
capitalizeWords("");
// ""