truncate method
Truncates a String
with more than length
characters.
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(3); // Returns 'con...'
Implementation
String? truncate(int length) {
if (this == null) {
return null;
}
if (this!.isEmpty) {
return this;
}
if (length <= 0) {
return this;
}
if (length > this!.length) {
return this;
}
return '${this!.substring(0, length)}...';
}