开发者

Issue escaping single quote in iOS with stringByReplacingOccurrencesOfString:widthString

开发者 https://www.devze.com 2023-01-21 02:19 出处:网络
NSString *test =@\"d\'escape\"; NSLog(@\"%@\", [test stringByReplacingOccurrencesOfString:@\"\'\" withString:@\"\\\'\"]);
 NSString *test =  @"d'escape";
 NSLog(@"%@", [test stringByReplacingOccurrencesOfString:@"'" withString:@"\'"]);

prints me this

2010-10-25 15:10:54.833 MyApp[7136:207] d'escape

What am I doing wrong ? I want to get this :

2010-10开发者_运维技巧-25 15:10:54.833 MyApp[7136:207] d\'escape


\ itself is a special character in C. You need to escape it in the source.

[test stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"]
//                                                           ^^
0

精彩评论

暂无评论...
验证码 换一张
取 消