Search code examples
nsrangenscharacterset

Exclude "@" sign from punctuationCharacterSet


I am trying to filter my attributedString this way, using this code. However, I want the rangeOfCharacterFromSet: to include all of its original values EXCEPT for the "@" sign. How would I do this?

NSRange rangeSpace = [filteredText rangeOfCharacterFromSet:[NSCharacterSet punctuationCharacterSet] options:NSCaseInsensitiveSearch range:NSMakeRange(range.location, filteredText.length - range.location)];

Solution

  • NSMutableCharacterSet *punctuationSet = [NSMutableCharacterSet punctuationCharacterSet];
    [punctuationSet removeCharactersInString:@"@"];
    
    NSRange searchRange = NSMakeRange(range.location, filteredText.length - range.location)
    
    NSRange rangeSpace = [filteredText rangeOfCharacterFromSet:punctuationSet 
                                                       options:NSCaseInsensitiveSearch
                                                         range:searchRange];