2 ooomiao oOOMIAO 于 2013.03.01 16:02 提问

过滤字符串的开头是数字或符号

怎么样对字符串的开头是数字或是符号进行过滤,然后给带NSPredicate的NSArray。

举个例子:

array = {"John", "Mary", "Aroldo", "1John", "+Mary"}

to newArray = {"1John," "+Mary"}

1个回答

g989_1314125
g989_1314125   2013.03.01 17:24
已采纳

有四种方法可以实现。每个方法都用了否定字符类,你的需求是要求数字或者符号,也就是非字母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);
}
Csdn user default icon
上传中...
上传图片
插入图片