@implementation NSString (URL)

- (NSString*)URLencodeWithEncoding:(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 = [[self 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];

[temp release];

return outStr;

}

-(NSString*)URLencodeWithEncodingUTF8{

return [self URLencodeWithEncoding:NSUTF8StringEncoding];

}

你可能感兴趣的:(NSString)