truncToLength method
Truncates the string to the given length
. Specify ellipsis
to append
to he truncated string if it is shorter than length
, e.g.
'Hello World'.truncToLength(5, ellipsis: '...') => 'Hello...'.
Implementation
String truncToLength(
int length, {
String ellipsis = '',
}) {
final temp = (this.length > length ? substring(0, length).trim() : this);
if (temp.length < length) return temp + ellipsis;
return temp;
}