37

I have a long NSString in which I m trying to replace special characters. Part of my string looks like this:

"veau (c\u00f4telette)","veau (filet)","agneau (gigot)","agneau (c\u00f4telette)","b**\u0153**uf (hach\u00e9)","porc (hach\u00e9)"

I would like to replace all the \u0153 with "oe". I've tried:

[response stringByReplacingOccurrencesOfString:@"\u0153" withString:@"oe"];

but it doesn't work.... I don't understand why!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nate
  • 7,606
  • 23
  • 72
  • 124

2 Answers2

87

The backslash is an escape character, so if you want to specify the actual backslash character in a string literal, you need to use two backslashes.

NSString *new = [old stringByReplacingOccurrencesOfString: @"\\u0153" withString:@"oe"];
Josh Freeman
  • 2,262
  • 16
  • 14
9

NSString is immutable, so the function generates a new string that you have to store:

NSString *new = [old stringByReplacingOccurrencesOfString:@"\u0153" withString:@"oe"];
eflorico
  • 3,589
  • 2
  • 30
  • 41