Shyamala Vardhan
Shyamala Vardhan

Reputation: 31

Nspredicate for matching string contains specified characters

Is there a way we can use nspredicate like this?

String = whvat

I want to search the string if it contains these characters [what]{4}. This will not return true for the above string since v is not found. I basically want a predicate to return true if a string contains 'n' specified characters. There can be other characters in the string but the specified should be definitely present. Order of characters does not matter. Even if I give [hwta] it should still give me the word whvat because 4 specified characters are found

Upvotes: 2

Views: 235

Answers (1)

Georg Seifert
Georg Seifert

Reputation: 71

This is not very elegant but you could try to make predicate for each character:

NSPredicate *p = [NSPredicate predicateWithFormat:@"self contains 'w' and self contains 'h' and self contains 'a' and self contains 't'"];
NSString *string = @"what";
NSLog(@"%d ", [p evaluateWithObject:string]);

Upvotes: 1

Related Questions