similarity method
Returns the number of matching characters of two strings.
If the isWordComparison
is true
then Return the number
of matching word of two strings.
The defualt Value of isWordComparison
is false.
Example :
print('Hello World'.similarity('Hello All')); // 5 => [H, e, l, o, ' ']
print('Hello World'.similarity('Hello All', isWordsComparison: true)); // 1
Implementation
int similarity(String matcher, {bool isWordComparison = false}) {
int similar = 0;
List<String> _subStr = [];
if (isWordComparison) {
_subStr = contains(' ') ? split(' ') : [this];
} else {
_subStr.addAll(iterable);
}
for (String str in _subStr) {
if (matcher.contains(str)) {
matcher = matcher.replaceAll(str, '');
similar++;
}
}
return similar;
}