Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Created December 4, 2014 22:05
Show Gist options
  • Save Air-Craft/4eab9f089c1c9628e57f to your computer and use it in GitHub Desktop.
Save Air-Craft/4eab9f089c1c9628e57f to your computer and use it in GitHub Desktop.
Sanitise string filtering out bad characters #strings #intermediate #objective-c
// Native...
NSCharacterSet* illegalFileNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@"/\\?%*|\"<>"];
NSString *sanitized = [[fileName componentsSeparatedByCharactersInSet:illegalFileNameCharacters] componentsJoinedByString:@""];
// With Overline (negative matching)
NSString *sanitized = [fileName stringByReplacingOccurrencesOfRegExpPattern:@"[^a-zA-Z0-9_\\+\\-]+" withString:@"_"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment