- (NSString*)encodeURL:(NSString *)string
{
NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL,
CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding])) autorelease]);
if (newString) {
return newString;
}
return @"";
}
decodeURL
CFStringRef CFURLCreateStringByReplacingPercentEscapesUsingEncoding(CFAllocatorRef allocator, CFStringRef origString, CFStringRef charsToLeaveEscaped, CFStringEncoding encoding)
+ (NSString *)decodeUTF8ToChinese:(NSString *)encodeStr;
{
return [encodeStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
+ (NSString *)encodeChineseToUTF8:(NSString *)encodeStr;
{
return [[NSString stringWithFormat:@"%@",encodeStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
+ (NSString *)encodeURL:(NSString *)string endcode:(NSStringEncoding)stringEncoding
{
NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL,
CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(stringEncoding)) autorelease]);
if (newString) {
return [newString autorelease];
}
return @"";
}
+ (NSString *)encodeURL:(NSString *)string
{
NSString *newString = NSMakeCollectable([(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL,
CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) autorelease]);
if (newString) {
return newString;
}
return @"";
}
+ (NSString*)encodeURL:(NSString *)originalString stringEncoding:(NSStringEncoding)stringEncoding
{
//! @ $ & ( ) = + ~ ` ; ' : , / ?
//%21%40%24%26%28%29%3D%2B%7E%60%3B%27%3A%2C%2F%3F
NSArray *escapeChars = [NSArray arrayWithObjects:@";" , @"/" , @"?" , @":" ,
@"@" , @"&" , @"=" , @"+" , @"$" , @"," ,
@"!", @"'", @"(", @")", @"*", nil];
NSArray *replaceChars = [NSArray arrayWithObjects:@"%3B" , @"%2F", @"%3F" , @"%3A" ,
@"%40" , @"%26" , @"%3D" , @"%2B" , @"%24" , @"%2C" ,
@"%21", @"%27", @"%28", @"%29", @"%2A", nil];
int len = [escapeChars count];
NSMutableString *temp = [[originalString
stringByAddingPercentEscapesUsingEncoding:stringEncoding]
mutableCopy];
int i;
for (i = 0; i < len; i++) {
[temp replaceOccurrencesOfString:[escapeChars objectAtIndex:i]
withString:[replaceChars objectAtIndex:i]
options:NSLiteralSearch
range:NSMakeRange(0, [temp length])];
}
NSString *outStr = [NSString stringWithString: temp];
return outStr;
}
+ (NSString*)encodeXML:(NSString *)originalString
{
//
// string = [string stringByReplacingOccurrencesOfString:@""" withString:@"\""];
// string = [string stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
// string = [string stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
// string = [string stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
// string = [string stringByReplacingOccurrencesOfString:@">" withString:@">"];
//不包换双引号:@""" @"\""
NSArray *escapeChars = [NSArray arrayWithObjects:@"'" , @"&" , @"<" ,
@">", nil];
NSArray *replaceChars = [NSArray arrayWithObjects:@"'", @"&" , @"<" ,
@">", nil];
int count = [escapeChars count];
NSMutableString *temp = [[originalString mutableCopy] autorelease];
for(int i = 0; i < count; i++)
{
[temp replaceOccurrencesOfString:[escapeChars objectAtIndex:i]
withString:[replaceChars objectAtIndex:i]
options:NSLiteralSearch
range:NSMakeRange(0, [temp length])];
}
NSString *outStr = [NSString stringWithString:temp];
return outStr;
}
+ (NSString*)decodeXML:(NSString *)originalString
{
NSArray *escapeChars = [NSArray arrayWithObjects:@""" , @"'", @"&" , @"<" ,
@">",@" ", nil];
NSArray *replaceChars = [NSArray arrayWithObjects:@"\"" , @"'" , @"&" , @"<" ,
@">", @"\r",nil];
int count = [escapeChars count];
NSMutableString *temp = [[originalString mutableCopy] autorelease];
for(int i = 0; i < count; i++)
{
[temp replaceOccurrencesOfString:[escapeChars objectAtIndex:i]
withString:[replaceChars objectAtIndex:i]
options:NSLiteralSearch
range:NSMakeRange(0, [temp length])];
}
NSString *outStr = [NSString stringWithString:temp];
return outStr;
}