toTitleCase2 property

String get toTitleCase2

Implementation

String get toTitleCase2 {
  if (this == null) return '';
  String spacedText = this!.replaceAll('_', ' ');

  // Convert camelCase to space-separated words
  spacedText = spacedText.replaceAllMapped(
    RegExp(r'(?<!^)(?=[A-Z])'),
    (Match match) => ' ${match.group(0)}',
  );

  // Convert the first letter of each word to uppercase
  String titleCaseText = spacedText.split(' ').map((word) {
    if (word.isEmpty) return '';
    return word[0].toUpperCase() + word.substring(1).toLowerCase();
  }).join(' ');

  return titleCaseText;
}