capitalizeWords method

String capitalizeWords()

Capitalizes the first letter of each word in the string.

Example:

'hello world'.capitalizeWords(); // 'Hello World'
'john doe smith'.capitalizeWords(); // 'John Doe Smith'

Implementation

String capitalizeWords() {
  if (isEmpty) return this;
  return split(' ')
      .map(
        (word) => word.isEmpty
            ? word
            : '${word[0].toUpperCase()}${word.substring(1).toLowerCase()}',
      )
      .join(' ');
}