URLQueryAllowedCharacterSet 去掉制定

2020-03-24  本文已影响0人  123_4567_8910

在程序中使用 URLQueryAllowedCharacterSet 对字符串进行编码时,有时因为特殊的原因,要去掉对某些字符的编码。
以下的两种方式对字符串编码的效果是相同的,所以当需要屏蔽指定字符时,秩序在第二种方法中去掉该字符即可。

  1. 正常使用方式
    NSCharacterSet * srti = [NSCharacterSet URLQueryAllowedCharacterSet];
    NSString * result = [string stringByAddingPercentEncodingWithAllowedCharacters:srti];
    NSLog(@"result: = %@",result);

2.手动指定方式

    NSString *charactersToEscape = @"`#%^{}\"[]|\\<>";
    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
    result = [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
    NSLog(@"result: = %@",result);

3.拓展常用的NSCharacterSet

URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}
URLHostAllowedCharacterSet      "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet  "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet      "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet     "#%<>[\]^`{|}
URLUserAllowedCharacterSet      "#%/:<>?@[\]^`
上一篇下一篇

猜你喜欢

热点阅读