假设我有一个如下数组:
shuffleArray = [[NSMutableArray alloc] initWithObjects:@"H",@"$",@"E",@"*",@"L",@"L",@"O", nil];
我想从这个数组中只获取特殊字符
并且在获取它们之后我想从数组中删除这些特殊字符
我该怎么做?
任何帮助都会很明显。
提前完成了......
答案 0 :(得分: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;
}]]);
答案 1 :(得分: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--;
}
}
试过这个并且它有效。希望能帮助到你。快乐的编码:)
答案 2 :(得分:0)
你使用NSPredicate:
示例:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"memberNumber == %@", [NSNumber numberWithInt:memberNumber]];
NSArray *matchingMembers = [members filteredArrayUsingPredicate:predicate];
Customer *customer = [matchingMembers lastObject];
NSLog(@"%@", customer.name);
Here是Apple的文档链接。
Here是另一种解释
答案 3 :(得分:-1)
您需要与特殊字符匹配的正则表达式,此链接可能会对您有所帮助。
你的正则表达式是这样的“[^ [A-Za-z] [0-9]]”