toTitle property

String get toTitle

Capitalizes the first letter of each word in the string while maintaining the -, _, and space characters.

If you want to ignore the _ and - characters, use the toTitleCase.

Example:

String input = 'example-string_for general use-sample.';
String title = input.toTitle;
print(title); // Output: 'Example-String_For General Use-Sample.'

Implementation

String get toTitle => splitMapJoin(
      RegExp('[-_]'),
      onMatch: (match) => match.group(0)!,
      onNonMatch: (subWord) =>
          subWord.isNotEmpty ? subWord.toTitleCase : subWord,
    );