last method

String last([
  1. int n = 1
])

Returns the last n characters of the String.

n is optional, by default it returns the last character of the String.

  • If n provided is longer than the String's length, the string will be returned.
  • If n is negative, it will return the string without the last n characters.

Example 1

String foo = 'hello world';
String firstChars = foo.last(); // returns 'd'

Example 2

String foo = 'hello world';
bool firstChars = foo.last(3); // returns 'rld'

Implementation

String last([int n = 1]) {
  if (n < 0) {
    n = length + n;
  }
  if (n <= 0) {
    return '';
  }

  if (isBlank || length < n) {
    return blankIfNull;
  }

  return substring(length - n, length);
}