我在从字符串中删除特殊字符时遇到问题。我使用了下面的代码。但是精巧的工作。请建议我更好的逻辑

- (NSString *)trimmedReciString:(NSString *)stringName
{
    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"-/:;()$&@\".,?!\'[]{}#%^*+=_|~<>€£¥•."];
    for (int i = 0; i < [stringName length]; i++) {
        unichar c = [stringName characterAtIndex:i];
        if ([myCharSet characterIsMember:c]) {
            NSLog(@"%@",[NSString stringWithFormat:@"%c",[stringName characterAtIndex:i]]);
            stringName =  [stringName stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%c",[stringName characterAtIndex:i]] withString:@""];

        }
    }
    return stringName;
}

最佳答案

NSString *s = @"$$$hgh$g%k&fg$$tw/-tg";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"-/:;()$&@\".,?!\'[]{}#%^*+=_|~<>€£¥•."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"String is: %@", s);

10-08 05:24