removeSingleCharacterWords method
Returns a new string with single-character words removed, or null if
the result is empty.
When trim is true (default), the result is trimmed. When
removeMultipleSpaces is true (default), consecutive spaces are
collapsed.
Example:
'a hello world'.removeSingleCharacterWords(); // 'hello world'
'I am 5 years old'.removeSingleCharacterWords(); // 'am years old'
'x y z'.removeSingleCharacterWords(); // null (all removed)
Implementation
@useResult
String? removeSingleCharacterWords({
bool trim = true,
bool removeMultipleSpaces = true,
}) {
if (isEmpty) {
return this;
}
String result = removeAll(_singleCharWordRegex);
if (removeMultipleSpaces) {
result = result.replaceAll(_consecutiveSpacesRegex, ' ');
}
if (trim) {
result = result.trim();
}
return result.isEmpty ? null : result;
}