NSString+M16String.m
1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// NSString+M16String.m
// Yohoboys
//
// Created by redding on 14-10-30.
// Copyright (c) 2014年 YOHO. All rights reserved.
//
#import "NSString+M16String.h"
@implementation NSString (M16String)
- (NSString *)encodeToPercentEscapeString
{
// Encode all the reserved characters, per RFC 3986
// (<http://www.ietf.org/rfc/rfc3986.txt>)
NSString *outputStr = (NSString *)
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return outputStr;
}
- (NSString *)decodeFromPercentEscapeString
{
NSMutableString *outputStr = [NSMutableString stringWithString:self];
[outputStr replaceOccurrencesOfString:@"+"
withString:@" "
options:NSLiteralSearch
range:NSMakeRange(0, [outputStr length])];
return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
@end