美文网首页
URLQueryAllowedCharacterSet 去掉制定

URLQueryAllowedCharacterSet 去掉制定

作者: 123_4567_8910 | 来源:发表于2020-03-24 18:12 被阅读0次

在程序中使用 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 去掉制定

      本文链接:https://www.haomeiwen.com/subject/uhrayhtx.html