secondCharacter method

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

Gets the second grapheme cluster (character).

Args:

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

Returns: The second character, or empty string if the string has fewer than 2 characters.

Implementation

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

  if (supportGraphemes) {
    final Characters chars = effective.characters;
    if (chars.length < 2) return '';
    return chars.elementAt(1);
  }

  // Without grapheme support, use runes (code points)
  final List<int> runeList = effective.runes.toList();
  if (runeList.length < 2) return '';
  return String.fromCharCode(runeList[1]);
}