countWords property

int countWords

Returns the word count in the given String.

The pattern is based on spaces.

Example

String foo = 'Hello dear friend how you doing ?';
int count = foo.countWords; // returns 6 words.

Implementation

int get countWords {
  if (this.isBlank) {
    return 0;
  }
  var words = this!.trim().split(RegExp(r'(\s+)'));
  // We filter out symbols and numbers from the word count
  var filteredWords = words.where((e) => e.onlyLatin.isNotEmpty);
  return filteredWords.length;
}