lastChars method

  1. @useResult
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

@useResult
String lastChars(int n) {
  if (n <= 0) {
    return '';
  }

  final int charLength = characters.length;
  if (n >= charLength) {
    return this;
  }

  return substringSafe(charLength - n);
}