NSPredicate versus NSString: Which is better/faster for finding superstrings?

后端 未结 2 1162
夕颜
夕颜 2021-02-10 16:16

I have a large number of strings that I\'m searching to see if a given substring exists. It seems there are two reasonable ways to do this.

Option 1: Use the NSStr

2条回答
  •  余生分开走
    2021-02-10 16:29

    Case (n): If you are having array of strings to test for a sub string, it will be better to use NSPredicate.

    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", substring];
    NSArray *resultArray = [originalArrayOfStrings filteredArrayUsingPredicate:regex];
    

    This will return array of strings which contain the sub string.

    If you useNSRange, in this case, you need to loop through all the string objects of the array manually, and obviously it will be slower than NSPredicate.

提交回复
热议问题