开发者

Evaluate/compare NSString with wildcards?

开发者 https://www.devze.com 2023-02-12 20:02 出处:网络
Hi I\'m trying to evaluate an NSString to see if it fits a certain criteria, which contains wildcards in the form of one or more asterix characters.

Hi I'm trying to evaluate an NSString to see if it fits a certain criteria, which contains wildcards in the form of one or more asterix characters.

Example:

NSString *filePath = @"file://Users/test/Desktop/file.txt";
开发者_StackOverflow社区NSString *matchCriteria = @"file://*/file.*";

I would like to see if filePath matches (fits?) matchCriteria, in this example it does. Does anybody know how I can go about doing this?

Many thanks!


In addition to NSRegularExpression, you can also use NSPredicate.

NSString *filePath = @"file://Users/test/Desktop/file.txt";
NSString *matchCriteria = @"file://*/file.*";

NSPredicate *pred = [NSPredicate predicateWithFormat:@"self LIKE %@", matchCriteria];

BOOL filePathMatches = [pred evaluateWithObject:filePath];

The Predicate Programming Guide is a comprehensive document desribing how predicates work. They can be particularly useful if you have an array of items that you want to filter based on some criteria.


NSString implements these methods:

@interface NSObject (NSComparisonMethods)
- (BOOL)isLike:(NSString *)object;
- (BOOL)isCaseInsensitiveLike:(NSString *)object;
@end

They are much faster than NSPredicate and NSRegularExpression.


You can use NSRegularExpression.

0

精彩评论

暂无评论...
验证码 换一张
取 消