11

I am receiving a nsstring that is not properly encoded like "mystring%201, where must be "mystring 1". How could I replace all characters that could be interpreted as UTF8? I read a lot of posts but not a full solution. Please note that nsstring is already encoded wrong and I am not asking about how to encode char sequence. Thank you.

Jaume
  • 3,672
  • 19
  • 60
  • 119

5 Answers5

34

- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding is what you want. basically use it like so:

newString = [myString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56
6
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Kjuly
  • 34,476
  • 22
  • 104
  • 118
Osama Khalifa
  • 363
  • 5
  • 10
2

Check the Strings and Non-ASCII Characters section of Formatting String Objects:

NSString *s = [NSString stringWithUTF8String:"Long \xe2\x80\x94   dash"];
A-Live
  • 8,904
  • 2
  • 39
  • 74
2
lblDate.text=[NSString stringWithCString:[[arrTripDetail valueForKey:Param_vCurrencySymbol] UTF8String] encoding:NSUTF8StringEncoding];

U can Convert & get Custom Emoji to string

eg :

input : \U20b9

Output: ₹

Mehul
  • 3,033
  • 23
  • 37
  • Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, *tailor your answers to the question.* – durron597 Sep 04 '15 at 13:40
1

Do you want just percent encoding/decoding or full URL encoding/decoding? -(NSString*)stringByReplacingPercentEscapesUsingEncoding: will work if it is just percent encoding, but if there is full URL encoding there (so, for example a space could be either %20 or +) then you'll need something like the url decoding in the three20 library (or search on there, there are lots of examples of how to do it such as URL decoding/encoding NSString).

Community
  • 1
  • 1
Michael
  • 1,213
  • 6
  • 9