upperCaseFirstChar method

  1. @useResult
String upperCaseFirstChar()

Converts the first character (grapheme cluster) of the string to uppercase, leaving the rest of the string unchanged.

If the string is empty, it returns an empty string. The first character is a whole grapheme cluster, so a leading emoji or a letter followed by a combining mark (e.g. 'e' + U+0301 becomes 'E' + U+0301) is kept intact.

Returns: A new string with the first character in uppercase, or an empty string if the original string is empty.

Example:

'mixedCase'.upperCaseFirstChar(); // Returns 'MixedCase'
'UPPERCASE'.upperCaseFirstChar(); // Returns 'UPPERCASE'
'👋ab'.upperCaseFirstChar();      // Returns '👋ab'
''.upperCaseFirstChar();        // Returns ''

Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
String upperCaseFirstChar() {
  if (isEmpty) return '';
  // Grapheme-safe split (see _splitFirstGrapheme) instead of this[0].
  final (String first, String rest) = _splitFirstGrapheme(this);

  return first.toUpperCase() + rest;
}