toSentenceCase method

String toSentenceCase()

Formats text to sentence case.

Example:

final text = "hello there. how are you?";
final formatted = text.toSentenceCase();
// returns: "Hello there. How are you?"

Implementation

String toSentenceCase() {
  if (trim().isEmpty) return this;

  final regex = RegExp(r'(^\s*|[.!?]\s+)(\p{L})', unicode: true);
  return toLowerCase().replaceAllMapped(regex, (match) {
    final prefix = match.group(1) ?? '';
    final char = match.group(2) ?? '';
    return '$prefix${char.toUpperCase()}';
  });
}