tags:

views:

78

answers:

2

My encoded string from the server looks like this: "it-strategy%20RZ%20U%20texas". How is it possible to decode this string back to "it-strategy RZ U texas"?

I have tried the method stringByReplacingPercentEscapesUsingEncoding: , but I have still the percentages.

+3  A: 

Check that you use right encoding type:

NSString *s = @"it-strategy%20RZ%20U%20texas";
NSLog(@"Decoded string: %@", [s stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding]);
Skie
You mean `NSUTF8StringEncoding`. Otherwise, non-ASCII characters will be encoded incorrectly (`NSASCIIStringEncoding` was treated as a synonym for `NSISOLatin1StringEncoding`, last I checked) or not at all (anything not in ISO-Latin-1).
Peter Hosey
A: 

I have tried the method stringByReplacingPercentEscapesUsingEncoding: , but I have still the percentages.

Are you looking at the string that message returned, or the string you sent that message to? If you're doing this:

NSString *myString = @"it-strategy%20RZ%20U%20texas";
[myString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

That's wrong.

A method named like stringByDoingSomething or arrayByAddingOrRemovingThings: will return a new object with the requested modification applied—the original object, myString in the above example, remains unchanged.

The percent escape sequences have been replaced in the string that stringByReplacingPercentEscapesUsingEncoding: returned. That's the string you need to look at:

NSString *unescapedString = [myString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Peter Hosey