lastChars method

String lastChars(
  1. int n
)

Get the last n graphemes (user-perceived characters) of a string.

Uses grapheme clusters for proper Unicode support, including emojis. Returns the full string if its length is less than n.

Implementation

String lastChars(int n) {
  // Handle invalid length.
  if (n <= 0) return '';
  final int charLength = characters.length;
  // If requested length is greater than or equal to string length, return the whole string.
  if (n >= charLength) return this;
  // Return the last n characters.
  return substringSafe(charLength - n);
}