lastChars method
Returns the last n
characters of the string.
If the string length is shorter than n
, it returns the entire string.
The string is also trimmed before extracting the last n
characters.
Example:
String? text = "Hello World";
print(text.lastChars(5)); // "World"
print(text.lastChars(10)); // "Hello World"
print(text.lastChars(15)); // "Hello World"
Implementation
String lastChars(int n) {
if (isEmptyOrNull) return '';
int charCount = n;
if (this!.length < n) {
charCount = this!.length;
}
return this!.trim().substring(this!.length - charCount);
}