Thomas
Thomas

Reputation: 1

How to decode a urlstring in the right way in cocoa-touch?

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.

Upvotes: 0

Views: 754

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96333

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];

Upvotes: 0

Skie
Skie

Reputation: 2012

Check that you use right encoding type:

NSString *s = @"it-strategy%20RZ%20U%20texas";
NSLog(@"Decoded string: %@", [s stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding]);

Upvotes: 3

Related Questions