学会NSCharacterSet,再也不怕各种字符串处理!!
url出现特殊字符处理-- stringByAddingPercentEncodingWithAllowedCharacters
几个用处
1.编码
2.字符串处理
/** 001 根据一个给定的字符串获取一个NSCharacterSet对象 */
+ (NSCharacterSet *)characterSetWithCharactersInString:(NSString *)aString;
// 使用实例,如上例!!
/** 002 相反字符串限制 【具体见接下的例子】 */
@property (readonly, copy) NSCharacterSet *invertedSet;
/** 003 常用快捷方法集合 (常用的,已满足大多数需求) */
+ controlCharacterSet
+ whitespaceCharacterSet //空格
+ whitespaceAndNewlineCharacterSet //空格和换行符
+ decimalDigitCharacterSet //0-9的数字
+ letterCharacterSet //所有字母
+ lowercaseLetterCharacterSet //小写字母
+ uppercaseLetterCharacterSet //大写字母
+ alphanumericCharacterSet //所有数字和字母(大小写不分)
+ punctuationCharacterSet //标点符号
+ newlineCharacterSet //换行
URLFragmentAllowedCharacterSet "#%<>[\]^`{|}
URLHostAllowedCharacterSet "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet "#%<>[\]^`{|}
URLUserAllowedCharacterSet "#%/:<>?@[\]^`
// url编码不对%编码
NSCharacterSet *queryAllowCharacterSet = [NSCharacterSet URLQueryAllowedCharacterSet];
NSMutableCharacterSet *mSet = [queryAllowCharacterSet mutableCopy];
[mSet addCharactersInString:@"%"];
NSString *encodeUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:mSet];
对`#^{}"[]|\<>这些字符编码
NSString *url = @"http://p0.meituan.net/mogu/272621e1bee4bfa74639ff9898ef01b2149126.jpg%40278w_200h_0e_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"`#^{}\"[]|\\<>"].invertedSet;
NSString *encodeUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:set];