capitalize method

  1. @useResult
String capitalize({
  1. bool makeLowerCaseRemaining = false,
})

Capitalizes the first letter of the string, leaving the rest of the string unchanged.

If lowerCaseRemaining is set to true, all characters after the first one are converted to lowercase.

Parameters:

  • lowerCaseRemaining: A boolean value. If true, converts the remaining characters to lowercase. Defaults to false.

The first letter is the first grapheme cluster, so a leading emoji ('👋ab' stays '👋ab') or a decomposed accent ('e' + U+0301 becomes 'E' + U+0301) is kept whole.

Returns: A new string with the first letter capitalized. Returns the original string if it is empty.

Example:

'lowercase'.capitalize(); // Returns 'Lowercase'
'mIxEd'.capitalize(lowerCaseRemaining: true); // Returns 'Mixed'
'👋ab'.capitalize();      // Returns '👋ab'
''.capitalize();        // Returns ''

Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
String capitalize({bool makeLowerCaseRemaining = false}) {
  if (isEmpty) {
    // failed null or empty check
    return this;
  }

  // Grapheme-safe split: this[0] + substringSafe(1) split surrogate pairs
  // and dropped combining marks (see _splitFirstGrapheme).
  final (String first, String rest) = _splitFirstGrapheme(this);

  // https://stackoverflow.com/questions/29628989/how-to-capitalize-the-first-letter-of-a-string-in-dart
  if (makeLowerCaseRemaining) {
    return '${first.toUpperCase()}${rest.toLowerCase()}';
  }

  return '${first.toUpperCase()}$rest';
}