NSString removing one quote in a string

It should be simple, but it does not work. I am trying to remove single quote marks from an NSString named parms using the following (stripped of irrelevant vars in the format string):

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

So, if parms contains "Mike Hat", I would expect newVar to contain "Mike Hat". Instead, it contains "Mike Hut."

+4
source share
2 answers

There should be more code for your code than you prove, but the following works fine:

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

Exit: Mikes Hat

Perhaps there is 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're using the wrong character, copy / paste this character into the string: '

+9
source

Just my two cents on the same problem I had in my code .... When I used a single quote on the keyboard to type 'in my code, it didn't work. But I was printing string values ​​to the console. When I copied and pasted the β€œfrom the console” symbol into my code, it worked. What is strange is that I use the same key on my keyboard to enter a string in a UITextField, so I really don't know why the same key turns into something else, but how I solved it.

0
source

Source: https://habr.com/ru/post/1338957/


All Articles