Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString removing single quote in string

This should be simple but it's not working. I am trying to strip single quote marks from an NSString named parms using the following (stripped of non-relevant vars in the format string):

NSString *newVar =[[NSString alloc] initWithFormat:@"%@", [parms stringByReplacingOccurrencesOfString:@"'" withString:@""]]; 

So if parms contains "Mike's Hat" I would expect that newVar would contain "Mikes Hat". Instead it contains "Mike's Hat".

like image 500
mlewis54 Avatar asked Jan 20 '23 13:01

mlewis54


1 Answers

There must be more to your code than you are proving, but the following works perfectly:

NSString *parms = @"Mike's Hat";
NSString *newVar =[parms stringByReplacingOccurrencesOfString:@"’" withString:@""];
NSLog(@"%@",newVar);

Output: Mikes Hat

There could be a possibility that the character ' may not be the same character in your parms string if the above does not work for you.

Turns out, you are using the wrong character copy/paste this character into your string:

like image 52
WrightsCS Avatar answered Jan 31 '23 07:01

WrightsCS