findCapitalizedWords method
Finds and returns a list of words from the string that are capitalized (start with an uppercase letter).
This method splits the string into words and then filters out the words that start with an uppercase letter.
The definition of a 'word' is determined by the words method of StringExtensions
.
Returns:
A List<String>? containing the capitalized words found in the string, or null
if no
capitalized words are found, or if the original string is empty.
Example:
'Find Capitalized Words'.findCapitalizedWords(); // Returns ['Find', 'Capitalized', 'Words']
'No capitalized words'.findCapitalizedWords(); // Returns null
''.findCapitalizedWords(); // Returns null
Implementation
List<String>? findCapitalizedWords() {
if (isEmpty) {
// failed null or empty check
return null;
}
return words()
?.where((String word) => word.isNotEmpty && word[0].isAllLetterUpperCase)
.toList()
.nullIfEmpty();
}