替换出现的NSString - iPhone

时间:2010-03-22 12:43:10

标签: iphone objective-c replace nsstring

我有一个很长的NSString,我试图替换特殊字符。我的部分字符串如下所示:

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

我想用“oe”替换所有\ u0153。我试过了:

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

但它不起作用....我不明白为什么!

2 个答案:

答案 0 :(得分:79)

反斜杠是escape character,因此如果要在字符串文字中指定实际的反斜杠字符,则需要使用两个反斜杠。

NSString *new = [old stringByReplacingOccurrencesOfString: @"\\u0153" withString:@"oe"];

答案 1 :(得分:7)

NSString是不可变的,因此该函数会生成一个必须存储的新字符串:

NSString *new = [old stringByReplacingOccurrencesOfString:@"\u0153" withString:@"oe"];
相关问题