Remove Special Characters in NSString

后端 未结 5 1928
野性不改
野性不改 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 <Foundation/Foundation.h>
    
    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;
    }
    
    0 讨论(0)
  • 2020-12-08 12:18

    I'm sure there's a more elegant solution, but for anyone trying to do this in Swift, here's what I did to make sure there were no special characters in my users' phone numbers.

    var phone = "+1 (555) 555 - 5555"
    var removeChars: NSCharacterSet = NSCharacterSet(charactersInString: "1234567890").invertedSet
    var charArray = phone.componentsSeparatedByCharactersInSet(removeChars)
    var placeholderString = ""
    var formattedPhoneNumber: String = placeholderString.join(charArray).stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
    

    The stringByTrimmingCharactersInSet might not be necessary.

    0 讨论(0)
  • 2020-12-08 12:26

    Per comment of @Rostyslav Druzhchenko from on the selected answer of @MadhuP:

    NSString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    NSCharacterSet *notAllowedChars = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    NSString *escapedString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
    NSLog (@"Result: %@", escapedString);
    

    This is the answer that will use alphanumericCharacterSet to handle multiple countries character set.

    0 讨论(0)
  • 2020-12-08 12:26

    Accepted answer in SWIFT (but not SWIFTY way):

     let notAllowedCharactersSet = NSCharacterSet(charactersInString: "ABCDEFGHIJKLMNOPQRSTUVWXYZ").invertedSet
     let filtered = (stringToFilter.componentsSeparatedByCharactersInSet(notAllowedCharactersSet) as NSArray).componentsJoinedByString("")
    
    0 讨论(0)
  • 2020-12-08 12:27
    NSString *unfilteredString = @"!@#$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] invertedSet];
    NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
    NSLog (@"Result: %@", resultString);
    

    TRY THIS IT MAY HELPS YOU

    0 讨论(0)
提交回复
热议问题