1

假设我有一个数组如下:

shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"$",@"E",@"*",@"L",@"L",@"O", nil];

我只想从此数组中获取特殊字符

此外,在获取它们之后,我想从数组中删除这些特殊字符

我该怎么做?

任何帮助都将是可观的。

提前谢谢...

4

4 回答 4

1

试试这个,

NSMutableArray *shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"$",@"E",@"*",@"L",@"L",@"O", nil];
    NSLog(@"Letters Array%@",[shuffleArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedString, NSDictionary *bindings) {
              return [[evaluatedString stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]] length]==0;

    }]]); 
    NSLog(@"Special Characters  Array%@",[shuffleArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedString, NSDictionary *bindings) {
        return [[evaluatedString stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]] length]!=0;

    }]]); 
于 2012-05-14T13:26:05.767 回答
1
NSMutableArray *shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"$",@"E",@"*",@"L",@"L",@"O", nil];
for(int i=0; i<[shuffleArray count];i++)
{
    unichar c = [[shuffleArray objectAtIndex:i] characterAtIndex:0];
    if (![[NSCharacterSet alphanumericCharacterSet] characterIsMember:c])
    {
        NSLog(@"%c",c);
        [shuffleArray removeObjectAtIndex:i];
        i--;
    }
}

试过这个,它的工作原理。希望能帮助到你。快乐编码:)

于 2012-05-14T13:28:14.077 回答
0

您为此使用 NSPredicate :

例子:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"memberNumber == %@", [NSNumber numberWithInt:memberNumber]];
NSArray *matchingMembers = [members filteredArrayUsingPredicate:predicate];

Customer *customer = [matchingMembers lastObject];
NSLog(@"%@", customer.name);

是 Apple 的文档链接。

是另一种解释

于 2012-05-14T13:17:31.943 回答
-1

您需要匹配特殊字符的正则表达式,此链接可能会对您有所帮助。

你的正则表达式是这样的“[^[A-Za-z][0-9]]”

如何使用正则表达式在 iOS 中获取匹配项?

于 2012-05-14T13:26:37.593 回答