substringCharacter method

String substringCharacter(
  1. int graphemeStart, [
  2. int? graphemeEnd
])

Gets a grapheme-aware substring.

Unlike the standard substring which works with code units, this method counts and extracts based on user-perceived characters (grapheme clusters).

Args:

  • graphemeStart: The starting grapheme index (inclusive).
  • graphemeEnd: The ending grapheme index (exclusive). If null, goes to end.

Returns: The substring between the grapheme indices, or empty string if invalid.

Implementation

String substringCharacter(int graphemeStart, [int? graphemeEnd]) {
  if (isEmpty) return '';

  final Characters chars = characters;
  final int len = chars.length;

  if (graphemeStart < 0 || graphemeStart > len) return '';

  final int end = graphemeEnd ?? len;
  if (end < graphemeStart || end > len) return '';
  if (graphemeStart == end) return '';

  return chars.skip(graphemeStart).take(end - graphemeStart).toString();
}