Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What characters are allowed in a iOS file name?

I'm looking for a way to make sure a string can be used as a file name under iOS. I'm currently in the section of the code that deletes incompatible characters. I'm wondering if I'm doing it right.

NSString *filename = @"A file name"; fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet controlCharacterSet]]; fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]]; 

I'm also wondering if there's already a method that validates a string as a file name.

Thank you for your advice!

like image 453
Constantino Tsarouhas Avatar asked May 23 '11 20:05

Constantino Tsarouhas


People also ask

Which characters are allowed in file names?

Supported characters for a file name are letters, numbers, spaces, and ( ) _ - , . *Please note file names should be limited to 100 characters. Characters that are NOT supported include, but are not limited to: @ $ % & \ / : * ? " ' < > | ~ ` # ^ + = { } [ ] ; !

What characters are allowed in Mac file names?

The names of files and folders in Mac OS X can use almost any character out of thousands of Unicode characters, which include symbols, arrows, and icons as well. There are only two characters you can't use: one is the colon (because it's used by the system).

Can you use symbols in file names?

Valid Special Characters for File and Folder NamesSpecial characters are symbols such as & (ampersand) or * (asterisk). Some require a shift key stroke, such as the special characters grouped in the number keys, while others do not, such as / (forward slash).


2 Answers

Use RegEx:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^a-zA-Z0-9_]+" options:0 error:nil]; filename = [regex stringByReplacingMatchesInString:filename options:0 range:NSMakeRange(0, filename.length) withTemplate:@"-"]; 
like image 182
Abhi Beckert Avatar answered Sep 20 '22 20:09

Abhi Beckert


I find this to be cleaner and probably much more performant. This is based on Angel Naydenov's solution, but first constructing Character set with all invalid characters and then calling components(separatedBy:) just once.

Swift 3 & 4

var invalidCharacters = CharacterSet(charactersIn: ":/") invalidCharacters.formUnion(.newlines) invalidCharacters.formUnion(.illegalCharacters) invalidCharacters.formUnion(.controlCharacters)  let newFilename = originalFilename     .components(separatedBy: invalidCharacters)     .joined(separator: "") 

Swift 2

let invalidCharacters = NSMutableCharacterSet(charactersInString: ":/") invalidCharacters.formUnionWithCharacterSet(NSCharacterSet.newlineCharacterSet()) invalidCharacters.formUnionWithCharacterSet(NSCharacterSet.illegalCharacterSet()) invalidCharacters.formUnionWithCharacterSet(NSCharacterSet.controlCharacterSet())  let filename = originalFilename     .componentsSeparatedByCharactersInSet(invalidCharacters)     .joinWithSeparator("") 
like image 22
Marián Černý Avatar answered Sep 19 '22 20:09

Marián Černý