uncommonCharacters method

Set<String> uncommonCharacters(
  1. String otherString, {
  2. bool caseSensitive = true,
  3. bool includeSpaces = false,
})

Returns a Set of the uncommon characters between the two Strings.

The String is case sensitive & sorted by default.

Example

String foo = 'Hello World';
List<String> uncommonLetters = foo.uncommonCharacters('World Hello'); // returns {};
String foo = 'Hello World';
List<String> uncommonLetters = foo.uncommonCharacters('World Hello!'); // returns {'!'};

Implementation

Set<String> uncommonCharacters(
  String otherString, {
  bool caseSensitive = true,
  bool includeSpaces = false,
}) {
  if (this.isBlank) {
    return {};
  }

  String processString(String input) {
    return (caseSensitive ? input : input.toLowerCase())
        .split('')
        .where((char) => includeSpaces || char != ' ')
        .join('');
  }

  final Set<String> thisSet = processString(this).split('').toSet();
  final Set<String> otherStringSet =
      processString(otherString).split('').toSet();

  final Set<String> uncommonSet = thisSet
      .union(otherStringSet)
      .difference(thisSet.intersection(otherStringSet));

  return uncommonSet;
}