新建一个NSString的分类
.h
@interface NSString (Unicode)
/**
Unicode转UTF8
@param string Unicode字符串
@return UTF8字符串
*/
+ (NSString *)utf8FromUnicode: (NSString *)string;
/**
UTF8转Unicode
@param string UTF8字符串
@return Unicode字符串
*/
+ (NSString *)utf8ToUnicode: (NSString *)string;
/**
URL编码
@param input 编码前字符串
@return 编码后字符串
*/
+ (NSString *)encodeToPercentEscapeString: (NSString *) input;
/**
URL解码
@param input 编码后字符串
@return 解码后的字符串
*/
+ (NSString *)decodeFromPercentEscapeString: (NSString *) input;
@end
.m
@implementation NSString (Unicode)
/**
Unicode转UTF8
@param string Unicode字符串
@return UTF8字符串
*/
+ (NSString *)utf8FromUnicode: (NSString *)string {
NSString *tempStr1 = [string stringByReplacingOccurrencesOfString:@"\\u"withString:@"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];
NSString *tempStr3 = [[@"\""stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString *returnStr = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NULL error:NULL];
return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n"withString:@"\n"];
}
/**
UTF8转Unicode
@param string UTF8字符串
@return Unicode字符串
*/
+(NSString *) utf8ToUnicode:(NSString *)string {
NSUInteger length = [string length];
NSMutableString *s = [NSMutableString stringWithCapacity:0];
for (int i = 0;i < length; i++) {
unichar _char = [string characterAtIndex:i];
//判断是否为英文和数字
if (_char <= '9' && _char >='0') {
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
} else if(_char >='a' && _char <= 'z') {
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
} else if(_char >='A' && _char <= 'Z') {
[s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
} else {
[s appendFormat:@"\\u%x",[string characterAtIndex:i]];
}
}
return s;
}
/**
URL编码
@param input 编码前字符串
@return 编码后字符串
*/
+ (NSString *)encodeToPercentEscapeString: (NSString *) input {
NSString *outputStr = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)input, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));
return outputStr;
}
/**
URL解码
@param input 编码后字符串
@return 解码后的字符串
*/
+ (NSString *)decodeFromPercentEscapeString: (NSString *) input {
NSMutableString *outputStr = [NSMutableString stringWithString:input];
[outputStr replaceOccurrencesOfString:@"+"
withString:@" "
options:NSLiteralSearch
range:NSMakeRange(0, [outputStr length])];
return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
@end