firstNWords method

  1. @useResult
String firstNWords(
  1. int n
)

Returns the first n words (split on whitespace). Returns empty string if n <= 0.

Implementation

@useResult
String firstNWords(int n) {
  if (n <= 0) return '';
  final List<String> words = trim().split(RegExp(r'\s+'));
  return words.take(n).join(' ');
}