last method
Returns the last n
characters of the String
.
n
is optional, by default it returns the first character of the String
.
If n
provided is longer than the String
's length, the string will be returned.
Faster than using
substring(length-n,length)
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 (this.isBlank || this.length < n || n < 0) {
return this;
}
return this.substring(this.length - n, this.length);
}