Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue escaping single quote in iOS with stringByReplacingOccurrencesOfString:widthString

Tags:

iphone

 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
like image 604
Thomas Joulin Avatar asked Oct 25 '10 13:10

Thomas Joulin


1 Answers

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

[test stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"]
//                                                           ^^
like image 94
kennytm Avatar answered Oct 04 '22 16:10

kennytm