iOS 字符串转JSON去除特殊字符的方法(去除表情)

- (NSString*)removeEmoji:(NSString *)username {

    

    NSString *regex = @"^[0-9.\u4e00-\u9fa5]+$";

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    NSString *temp = nil;

    

    for(int i = 0; i < [username length]; i++)

    {

        temp = [username substringWithRange:NSMakeRange(i, 1)];

        if ([predicate evaluateWithObject:temp]) {

            NSLog(@"%@", temp);

            NSLog(@"This character is OK");

        } else {

            NSRange range = NSMakeRange(i, 1);

            username = [username stringByReplacingCharactersInRange:range withString:@" "];

        }

    }

    

    NSString *withoutEmojiUsername = [username stringByReplacingOccurrencesOfString:@" " withString:@""];

    

    return withoutEmojiUsername;

}

你可能感兴趣的:(IOS)