words static method

String words(
  1. String value, [
  2. int words = 100,
  3. String end = '...'
])

Limits value to a number of words.

Implementation

static String words(String value, [int words = 100, String end = '...']) {
  final match = RegExp(
    r'^\s*(?:\S+\s*){1,' + words.toString() + r'}',
  ).firstMatch(value);
  if (match == null || match.group(0)!.length >= value.length) return value;
  return '${match.group(0)!.trimRight()}$end';
}