truncate method

String truncate(
  1. int maxLength, {
  2. String ellipsis = '...',
})

Truncates to maxLength chars, appending ellipsis if cut.

'Hello World'.truncate(7); // 'Hell...'

Implementation

String truncate(int maxLength, {String ellipsis = '...'}) {
  assert(
    maxLength > ellipsis.length,
    'maxLength must exceed ellipsis length',
  );
  if (length <= maxLength) return this;
  return substring(0, maxLength - ellipsis.length) + ellipsis;
}