CyrefJS
CCyrefJS
فا
Back to documentation
String/Since 1.38.0

capitalizeWords

Capitalizes the first character of every space-separated word and converts the remaining characters of each word to lowercase.

capitalizeWords(value):string

Behavior

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

valuestringrequired

The string whose words should be capitalized.

Returns

string

A string with the first character of each space-separated word uppercase and the remaining characters lowercase.

Examples

01

Basic usage

Capitalize every word in a sentence.

TypeScript
import { capitalizeWords } from "@cyref/js";

capitalizeWords("hello world");
// "Hello World"
02

Uppercase input

Each word is normalized with an uppercase first character and lowercase remaining characters.

TypeScript
capitalizeWords("HELLO WORLD");
// "Hello World"
03

Mixed case

Mixed casing is normalized for every word.

TypeScript
capitalizeWords("hELLo WoRLD");
// "Hello World"
04

Single word

A single word is capitalized and its remaining characters are lowercased.

TypeScript
capitalizeWords("hELLO");
// "Hello"
05

Multiple spaces

Empty parts created by consecutive spaces are preserved.

TypeScript
capitalizeWords("hello  world");
// "Hello  World"
06

Leading and trailing spaces

Leading and trailing spaces are preserved.

TypeScript
capitalizeWords("  hello world  ");
// "  Hello World  "
07

Empty string

An empty string is returned unchanged.

TypeScript
capitalizeWords("");
// ""
Built with@cyref/js·Official documentation