removeLastWord method
Removes the last word and its surrounding punctuation, leaving the rest of the string.
Useful for trimming suffixes or processing filenames.
Example:
'This is a sentence.'.removeLastWord() // 'This is a'
'file.txt'.removeLastWord() // 'file'
'single'.removeLastWord() // ''
Implementation
String removeLastWord() {
return trim()
== '' ?
'' :
replaceFirst(RegExp('[^$wordChars]*[$wordChars]+(?:-[$wordChars]+)*[^$wordChars]*\$'), '');
}