truncate method
Truncates the String when more than length characters exist.
length must be more than 0.
If length > String.length the same String is returned without truncation.
Example:
String f = 'congratulations';
String truncated = f.truncate(4); // 'cong...'
Implementation
String truncate(int length) {
if (isBlank || length <= 0 || length >= this.length) {
return this;
}
return '${substring(0, length)}...';
}