Is there way to take a NSString and turn it into a safe version that can be used as a filename to save to the user Documents directory on the iPhone.
I’m currently doing something like this:
NSString *inputString = @"This is sample text which may have funny chars and spaces in.";
NSInteger len = [inputString length];
NSString *newFilename = [[inputString substringToIndex:MIN(20, len)] stringByAppendingPathExtension:@"txt"];
This currently leaves me with something like:
This is sample text .txt
Need to make sure characters that are not allowed in filenames and stripped out too.
You’ll probably end up with some regex or something. Simply because it’s too dangerous to use a
except-filter (you may miss some illegal chars).Therefor I’ld recommend you to use RegexKitLite (http://regexkit.sourceforge.net/RegexKitLite/), combined with the following line of code:
This will replace all characters except A-Z, a-z and 0-9 =)!