Home > Back-end >  Dart, get all n number letter values from a list
Dart, get all n number letter values from a list

Time:11-05

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?

CodePudding user response:

List nLetters(List 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]

  • Related