URLQueryAllowedCharacterSet 去掉制定字符

在程序中使用 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      "#%/:<>?@[\]^`

你可能感兴趣的:(URLQueryAllowedCharacterSet 去掉制定字符)