问题描述
- 过滤字符串的开头是数字或符号
- 怎么样对字符串的开头是数字或是符号进行过滤,然后给带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);}
时间: 2024-09-30 03:57:24