Reputation: 21
Sample 1:
tags = ['what','play','school']
string = "what is your name?"
Output = ['what']
Sample 2:
tags = ['what is','play football','school','food']
string = "what is your school name? Do you play football there?"
Output = ['what is','school',"play football"]
How can I achieve this in flutter?
Upvotes: 0
Views: 231
Reputation: 310
void main() {
var tags = ['what is', 'play football', 'school', 'food'];
String string = "what is your school name? Do you play football there?";
print(tags.where((element)=> string.contains(element)));
}
Upvotes: -1
Reputation: 17790
You can use where to find all words present in the string.
void main() {
List<String> tags = ['what', 'play', 'school'];
String _string = "what is your name?";
List<String> commonWords = [...tags.where(_string.contains)];
print(commonWords); // [what]
}
Upvotes: 3