Skip to content

Instantly share code, notes, and snippets.

@karosLi
Forked from MadeBugs/StringEncode.m
Created October 24, 2024 03:32
Show Gist options
  • Save karosLi/f1aedf94138668a233e9e295c9c1ef5f to your computer and use it in GitHub Desktop.
Save karosLi/f1aedf94138668a233e9e295c9c1ef5f 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