Remove double quotes from NSString

How to remove double quotes from NSString. Example:

//theMutableString: "Hello World" [theMutableString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:(NSRange){0,[theMutableString length]}] 

It doesn't seem to have worked, maybe since I had to hide "in replaceOccurrencesOfString?"

+4
source share
6 answers

Use the NSMakeRange function instead of your translation. This will work:

 [mString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mString length])]; 
+16
source

How about this?

 NSCharacterSet *quoteCharset = [NSCharacterSet characterSetWithCharactersInString:@"\""]; NSString *trimmedString = [toBeTrimmedString stringByTrimmingCharactersInSet:quoteCharset]; 
+4
source

Assuming the mString bit is a typo. I ran this code and the answer was as expected

 NSMutableString * theMutableString = [[NSMutableString alloc] initWithString:@"\"Hello World!\""]; NSLog(@"%@",theMutableString); [theMutableString replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:(NSRange){0,[theMutableString length]}]; NSLog(@"%@",theMutableString); [theMutableString release]; 

Exit

 2010-01-23 15:49:42.880 Stringtest[2039:a0f] "Hello World!" 2010-01-23 15:49:42.883 Stringtest[2039:a0f] Hello World! 

So mString was a typo in your question, then your code is correct and the problem is elsewhere. If mString is a typo in your code, this will be a problem.

+2
source

What happens if you use NSMakeRange(0, [theMutableString length]) instead of trying to create an inline structure?

0
source

I am working fine on my end (I copied your replacement string), both with NSMakeRange and with inline structure. Are you sure the quotes in your NSString are really ASCII 0x22 characters?

0
source

you can use

 string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [string length])]; 
0
source

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


All Articles