假设我有一个数组如下:
shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"$",@"E",@"*",@"L",@"L",@"O", nil];
我只想从此数组中获取特殊字符
此外,在获取它们之后,我想从数组中删除这些特殊字符
我该怎么做?
任何帮助都将是可观的。
提前谢谢...
假设我有一个数组如下:
shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"$",@"E",@"*",@"L",@"L",@"O", nil];
我只想从此数组中获取特殊字符
此外,在获取它们之后,我想从数组中删除这些特殊字符
我该怎么做?
任何帮助都将是可观的。
提前谢谢...
试试这个,
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;
}]]);
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--;
}
}
试过这个,它的工作原理。希望能帮助到你。快乐编码:)
您为此使用 NSPredicate :
例子:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"memberNumber == %@", [NSNumber numberWithInt:memberNumber]];
NSArray *matchingMembers = [members filteredArrayUsingPredicate:predicate];
Customer *customer = [matchingMembers lastObject];
NSLog(@"%@", customer.name);
这是 Apple 的文档链接。
这是另一种解释