我们如何在URL中传递字符串Mr.X和Mr.Y.
我试过这个,但这个人做了所有的字符,但是&符号.
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Holtwick.. 36
甚至更短:
@implementation NSString (Escaping) - (NSString*)stringWithPercentEscape { return [(NSString *) CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)[[self mutableCopy] autorelease], NULL, CFSTR("?=,!$&'()*+;@?\n\"<>#\t :/"), kCFStringEncodingUTF8) autorelease]; } @end
在这里它再次作为ARC符合内联函数帮助器:
#if __has_feature(objc_arc) static inline NSString *hxURLEscape(NSString *v) { static CFStringRef _hxURLEscapeChars = CFSTR("?=,!$&'()*+;@?\r\n\"<>#\t :/"); return ((__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (__bridge CFStringRef)[v mutableCopy], NULL, _hxURLEscapeChars, kCFStringEncodingUTF8)); } #endif
更短更可怕:) (2认同)
实际上,我认为我们根本不需要复制它,可以用`self`替换`[[self mutableCopy] autorelease]`.关于它的更多细节可以在`CFURLCreateStringByAddingPercentEscapes`的文档中找到http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFURLRef/Reference/reference.html (2认同)
Rog.. 32
-stringByAddingPercentEscapesUsingEncoding:
不执行完整的转义编码.您应该使用手动添加编码-replaceOccurrencesOfString:withString:
这是一个完整的列表(镜像Gamecat的列表),最初建议在https://devforums.apple.com/message/15674#15674.正如尼克指出的那样,这很昂贵,所以不要只考虑完整列表而不考虑你逃避的真正要求.
NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [escaped replaceOccurrencesOfString:@"$" withString:@"%24" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:wholeString];
此代码肯定会对您的URL进行哈希处理.
甚至更短:
@implementation NSString (Escaping) - (NSString*)stringWithPercentEscape { return [(NSString *) CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)[[self mutableCopy] autorelease], NULL, CFSTR("?=,!$&'()*+;@?\n\"<>#\t :/"), kCFStringEncodingUTF8) autorelease]; } @end
在这里它再次作为ARC符合内联函数帮助器:
#if __has_feature(objc_arc) static inline NSString *hxURLEscape(NSString *v) { static CFStringRef _hxURLEscapeChars = CFSTR("?=,!$&'()*+;@?\r\n\"<>#\t :/"); return ((__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (__bridge CFStringRef)[v mutableCopy], NULL, _hxURLEscapeChars, kCFStringEncodingUTF8)); } #endif
-stringByAddingPercentEscapesUsingEncoding:
不执行完整的转义编码.您应该使用手动添加编码-replaceOccurrencesOfString:withString:
这是一个完整的列表(镜像Gamecat的列表),最初建议在https://devforums.apple.com/message/15674#15674.正如尼克指出的那样,这很昂贵,所以不要只考虑完整列表而不考虑你逃避的真正要求.
NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [escaped replaceOccurrencesOfString:@"$" withString:@"%24" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:wholeString]; [escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:wholeString];
此代码肯定会对您的URL进行哈希处理.