Search code examples
iosobjective-cxcodensstringnsstringencoding

How to replace the \u00e2\u0080\u0099 this string into ' in iOS


I got a value from server and i stored it in NSString object. In that string "'" is replaced with \u00e2\u0080\u0099. I tried to replace this by using replaceOccurencesOfString. But it shows an error. Herewith i displayed my code and error.

 [mutstr_Description replaceOccurrencesOfString:@"\u00e2\u0080\u0099" withString:@"'" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutstr_Description length])];

enter image description here

Help to solve this issue.


Solution

  • Assuming your string actually has the literal characters \, u, 0, etc., you need to do this:

    [mutstr_Description replaceOccurrencesOfString:@"\\u00e2\\u0080\\u0099" withString:@"'" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutstr_Description length])];
    

    You need to escape the backslashes.

    Update - perhaps your string actually has those control characters. Try this:

    NSString *controlChars = [NSString stringWithFormat:@"%C%C%C", 0xe2, 0x80, 0x99];
    [mutstr_Description replaceOccurrencesOfString:controlChars withString:@"'" options:NSLiteralSearch range:NSMakeRange(0, [mutstr_Description length])];