rob180
rob180

Reputation: 901

IOS NSString decode not working

I am receiving a string from the internet but i cant find a way to transform the special char and them show in on an alert box. This is the string:

@"Sorry, your search doesn\u0027t have any result"

i know i could do something like this to work around

[response stringByReplacingOccurrencesOfString:@"\u0027" withString:@"'"];

but i wanted a solution that would work for any character

i have already tried several solution but none seam to work, and i don't know why

Upvotes: 2

Views: 203

Answers (2)

Paresh
Paresh

Reputation: 138

Try

NSString* string = [NSString stringWithUTF8String:[response cStringUsingEncoding:NSUTF8StringEncoding]];

This should work.

Upvotes: 0

Daniel Klöck
Daniel Klöck

Reputation: 21147

You can do it like this:

NSString *escapedStr = @"Sorry, your search doesn\\u0027t have any result";
NSString *unescapedStr = [NSString
            stringWithCString:[escapedStr cStringUsingEncoding:NSUTF8StringEncoding]
            encoding:NSNonLossyASCIIStringEncoding];
NSLog(@"unescapedStr = %@", unescapedStr);

Upvotes: 3

Related Questions