Skip to content

Instantly share code, notes, and snippets.

@MadeBugs
Last active July 24, 2019 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MadeBugs/9afd993ad564418331b4f8e2eca06501 to your computer and use it in GitHub Desktop.
Save MadeBugs/9afd993ad564418331b4f8e2eca06501 to your computer and use it in GitHub Desktop.
特殊字符转码
//特殊字符转码
- (NSString *)encodeWithString:(NSString *)string {
NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
}
//URL中包含中文的连接转码
NSString *escapedPath = [@"http://www.baidu.com?中文" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
// URL中包含#,但是又不能把#格式化
NSString *escapedPath = [@"http://www.baidu.com/#/name=维基百科" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"%^{}\"[]|\\<>"].invertedSet];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment