1、通过NSCharacterSet处理字符串
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"123"] invertedSet];
NSString * originalStr = @"1y3j g425";
NSString * finStr = [[originalStr componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""];
其中 invertedSet 为倒置字符集
2、通过NSCharacterSet进行特殊字符转码
原始转码为:
NSString *outputStr = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)input,NULL,(CFStringRef)@"!*'();:@&=+ $,/?%#[]",kCFStringEncodingUTF8));
NSCharacterSet转码:
NSString * originalStr2 = @"https://www.baidu.com";
NSString * characters = @"#[]@!$'()*+,;\"<>%{}|^~`/";
NSString * encodeStr = [originalStr2 stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:characters] invertedSet]];
或者通过系统提供的URLQueryAllowedCharacterSet等进行转码。
3、解码
encodeStr = [encodeStr stringByRemovingPercentEncoding];