Jan
Jan

Reputation: 367

Dart, get all n number letter values from a list

Let's say I have a list

List test = ['hello', 'two', 'mountain', 'day',  'tomorrow', 'bye', 'sad', 'yesterday'];

ExpectedResult = ['two', 'day', 'bye', 'sad'];

I want to extract all values from a list that are of certain number of characters long, in this case all result words that I'm looking have three characters, is there a simple way of doing this?

Upvotes: 0

Views: 46

Answers (1)

Tyler Liu
Tyler Liu

Reputation: 1069

List<String> nLetters(List<String> list, int n) {
    return list.where((word) => word.length == n).toList();
}

Calling nLetters(['hello', 'two', 'mountain', 'day', 'tomorrow', 'bye', 'sad', 'yesterday'], 3) will produce [two, day, bye, sad]

Upvotes: 3

Related Questions