firstWord method

String firstWord()

Extracts the first word from the string.

Returns an empty string if no word characters are found. Handles accented characters and hyphenated words.

Example:

'This is a sentence.'.firstWord()          // 'This'
'  Hello  World'.firstWord()               // 'Hello'
'well-being matters'.firstWord()           // 'well-being'
'!!!no words!!!'.firstWord()               // ''

Implementation

String firstWord() {
  final match = RegExp('^[^$wordChars]*([$wordChars]+(?:-[$wordChars]+)*)').firstMatch(this);
  return match?.group(1)?.trim() ?? '';
}