码迷,mamicode.com
首页 > Web开发 > 详细

对于URL中文和特殊字符的处理方法

时间:2016-04-08 06:31:21      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

1.中文的处理方法

NSString* string1 = @"https://www.cloudsafe.com/文件夹";
    
NSString* string2 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* string3 = [string2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
NSString* string4 = [string2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* string5 = [string3 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
NSString* string6 = [string4 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* string7 = [string5 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
输出结果: string5 & 6 重复编码encode
 
string1:https://www.cloudsafe.com/文件夹
 
string2:https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9
string3:https://www.cloudsafe.com/%25E6%2596%2587%25E4%25BB%25B6%25E5%25A4%25B9
 
string4:https://www.cloudsafe.com/文件夹
string5:https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9
 
string6:https://www.cloudsafe.com/文件夹
string7:https://www.cloudsafe.com/文件夹

2.特殊字符处理方式

-(NSString *)UrlEncodedString:(NSString * )sourceText
{
    NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)sourceText ,NULL ,CFSTR("!*‘();:@&=+$,/?%#[]") ,kCFStringEncodingUTF8));
    return result;
}

 

对于URL中文和特殊字符的处理方法

标签:

原文地址:http://www.cnblogs.com/fuunnyy/p/5366299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!