Mst. Murshida Khnom
Mst. Murshida Khnom

Reputation: 21

Get Common substring between list and string Flutter

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

Answers (2)

AlishanMuhammadAmin
AlishanMuhammadAmin

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

Kaushik Chandru
Kaushik Chandru

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

Related Questions