-
-
Save karosLi/f1aedf94138668a233e9e295c9c1ef5f to your computer and use it in GitHub Desktop.
特殊字符转码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//特殊字符转码 | |
- (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