Remove Special Characters in NSString

后端 未结 5 1939
野性不改
野性不改 2020-12-08 11:23

i am convert paragraph into words it contains many special characters like

\"  , \"  . `

how to remove this characters in nsstring and get

5条回答
  •  难免孤独
    2020-12-08 12:16

    There are numerous ways of dealing with this. As an example, here's a solution using regular expressions. This is just an example. We don't know the entire range of special characters that you want to remove.

    #import 
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"[,\\.`\"]"
                                                                                        options:0
                                                                                          error:NULL];
            NSString *sampleString = @"The \"new\" quick brown fox, who jumped over the lazy dog.";
            NSString *cleanedString = [expression stringByReplacingMatchesInString:sampleString
                                                                           options:0
                                                                             range:NSMakeRange(0, sampleString.length)
                                                                      withTemplate:@""];
            printf("cleaned = %s",[cleanedString UTF8String] );
    
        }
        return 0;
    }
    

提交回复
热议问题