truncate method

String truncate([
  1. int maxLength = 90
])

Truncates string with ellipsis, collapses multiple spaces to one.

Example:

final text = 'hello there, how are you?';
final compact = text.truncate(10);
// returns: 'Hello there...'

Implementation

String truncate([int maxLength = 90]) {
  final text = trim().replaceAll(RegExp(r'\s+'), ' ');
  if (text.length <= maxLength) return text;
  return '${text.substring(0, maxLength)}...';
}