removeArticles function

String removeArticles (
  1. String sentence
)

Article"s are the words "a", "an", and "the".

"Words" are anything separated by whitespace.

Ignoring special characters will result in hi! matching hi.

When matching words, duplicates won't be considered. So if the String being matched contains 4 "hi"s and the String being matched against contains 3 "hi"s, only 3 of the "hi"s would be counted. Returns sentence without "a", "an", and "the".

removeArticles('A multiple   spaced  the    '); // 'multiple   spaced     '

Implementation

/// Returns [sentence] without "a", "an", and "the".
///
/// ```dart
/// removeArticles('A multiple   spaced  the    '); // 'multiple   spaced     '
/// ```
String removeArticles(String sentence) {
  var words = sentence.split(' ');
  for (var word in sentence.split(' ')) {
    var modified = word.replaceAll(RegExp(r'[^a-zA-Z]*'), '').toLowerCase();
    if (['a', 'an', 'the'].contains(modified)) {
      words.remove(word);
    }
  }
  return words.join(' ');
}