Home > database >  Flutter search 'keywords' inside a String?
Flutter search 'keywords' inside a String?

Time:04-14

I want to search for a particular keyword in a String. For example, there is a string 'rajasthan' now if I want to search using 'rj', it will not detect anything, but if I search using 'raj' then it will. So what to do in this case?

My current way of searching:

String raj = 'rajasthan';
  bool searchRaj = raj.contains('raj');
  bool searchRJ = raj.contains('rj');
  
  print('raj contains $searchRaj');
  print('rj contains $searchRJ');

Dartpad: https://dartpad.dev/?id=40472ce8cd82bd87ba916ea2f3e7eff9

CodePudding user response:

Here you can find the example of how to achieve this.

void main() {

    String raj = 'rajasthan';
    String search1 = 'raj';
    String search2 = 'rj';
    var searchList = raj.split("").toSet();
    var searchRAJ = search1.split("").toSet();
    var searchRJ = search2.split("").toSet();

    bool result1 = searchList.containsAll(searchRAJ);
    bool result2 = searchList.containsAll(searchRJ);
    print('raj contains $result1');
    print('rj contains $result2');

}
  • Related