iOS 字符编码

iOS 9之后苹果推荐使用编码方法

[str  stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]]
  • 其中对NSCharacter​Set类就很值得研究
  • 此处用到URLPathAllowedCharacterSet并不在 NSCharacterSet.h类文件中

//    URLFragmentAllowedCharacterSet    "#%<>^`\[]{|}
//    URLPasswordAllowedCharacterSet    "#%<>^`\[]{|}/:?@
//    URLPathAllowedCharacterSet        "#%<>^`\[]{|};?
//    URLQueryAllowedCharacterSet       "#%<>^`\[]{|}
//    URLUserAllowedCharacterSet        "#%<>^`\[]/:?@
//    URLHostAllowedCharacterSet        "#%<>^`\{|}/?@
  • 对于中文、中文标点符号以及空格都是要编码的
  • 每个属性后面对应的特殊符号也都是要编码的
  • emoji表情也是都要编码的
  • 对于字母、数字英文标点符号+-/* 都直接显示不编码
  • 其中对应后面没有提到的特殊字符大都不编码
NSString *string = @"$&1111aaaaa#";
NSString *unicode = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
屏幕快照 2019-03-29 16.25.53.png

打印结果:$&1111%F0%9F%98%86aaaaa%23
由此可以看出表情 和 # 是进行编码 的其他字符是不进行编码。

你可能感兴趣的:(iOS 字符编码)