怎麼樣對字符串的開頭是數字或是符號進行過濾,然後給帶NSPredicate的NSArray。
舉個例子:
array = {"John", "Mary", "Aroldo", "1John", "+Mary"}
to newArray = {"1John," "+Mary"}
有四種方法可以實現。每個方法都用了否定字符類,你的需求是要求數字或者符號,也就是非字母character。
- (void)testFilterArray0
{
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[^a-zA-Z].*"];
filtered = [unfiltered filteredArrayUsingPredicate:predicate];
STAssertTrue([filtered isEqualToArray:expected], nil);
}
- (void)testFilteredArray1
{
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[^\\p{L}].*"];
filtered = [unfiltered filteredArrayUsingPredicate:predicate];
STAssertTrue([filtered isEqualToArray:expected], nil);
}
- (void)testFilteredArray2
{
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[^\\p{Letter}].*"];
filtered = [unfiltered filteredArrayUsingPredicate:predicate];
STAssertTrue([filtered isEqualToArray:expected], nil);
}
- (void)testFilteredArray3
{
predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[^\\p{General_Category=Letter}].*"];
filtered = [unfiltered filteredArrayUsingPredicate:predicate];
STAssertTrue([filtered isEqualToArray:expected], nil);
}