wordsCount function

int wordsCount(
  1. String? text, [
  2. WordCountConfig config = const WordCountConfig()
])

Counts words in text and returns the count as an integer.

This is a convenience function that calls wordsDetect and returns only the word count. Use this when you only need the number of words.

Supports 85+ languages with proper handling for different writing systems. Returns 0 for null, empty, or whitespace-only text.

Example:

int count = wordsCount('Hello World'); // 2
int chinese = wordsCount('你好世界'); // 4
int mixed = wordsCount('Hello, 你好!'); // 3

// With configuration
const config = WordCountConfig(punctuationAsBreaker: true);
int count2 = wordsCount("don't", config); // 2

Implementation

int wordsCount(
  String? text, [
  WordCountConfig config = const WordCountConfig(),
]) {
  final result = wordsDetect(text, config);
  return result.count;
}