containsAllCharacters method

bool containsAllCharacters(
  1. String characters
)

Checks whether all characters are contained in the String.

The method is case sensitive by default.

Example

String foo = 'Hello World';
bool containsAll = foo.containsAllCharacters('Hello'); // returns true;
String foo = 'Hello World';
bool containsAll = foo.containsAllCharacters('Hello!'); // returns false;

Implementation

bool containsAllCharacters(String characters) {
  if (this.isBlank) {
    return false;
  }
  final Map<String, int> letterCounts = {};

  this.split('').forEach((letter) {
    letterCounts[letter] = (letterCounts[letter] ?? 0) + 1;
  });

  for (final letter in characters.split('')) {
    if (letterCounts[letter] == null || letterCounts[letter]! <= 0) {
      return false;
    }
    letterCounts[letter] = letterCounts[letter]! - 1;
  }

  return true;
}