首页/Apple开发/字符串编码与解码函数(UTF8)/

字符串编码与解码函数(UTF8)

2013-05-04
2006

字符串编码:

 

- (NSString *)urlEncode:(NSString *)oldString{  
     CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)oldString, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);  
    return [NSString stringWithFormat:@"%@", escaped];  
}

 



字符串解码:

 

- (NSString *)urlDecode:(NSString *)oldString{  
    NSMutableString *unescaped = [NSMutableString stringWithString:oldString];  
    [unescaped replaceOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [unescaped length])];  
    return [unescaped stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
}

 

Copyright © 2021 技术探索 All Rights Reserved.