firstCharacter method

String firstCharacter({
  1. bool trim = true,
  2. bool supportGraphemes = true,
})

Gets the first grapheme cluster (character).

Args:

  • trim: If true (default), trims the string before getting the first character.
  • supportGraphemes: If true (default), handles multi-byte Unicode characters as single grapheme clusters (e.g., emoji with skin tones, family emojis).

Returns: The first character, or empty string if the string is empty.

Implementation

String firstCharacter({bool trim = true, bool supportGraphemes = true}) {
  final String effective = trim ? this.trim() : this;
  if (effective.isEmpty) return '';

  if (supportGraphemes) {
    return effective.characters.first;
  }

  // Without grapheme support, return the first rune (code point)
  return String.fromCharCode(effective.runes.first);
}