How to make an NSString path (file name) safe

后端 未结 7 723
陌清茗
陌清茗 2021-02-01 01:52

I\'m using very tricky fighting methods :) to make a string like Fi?le*/ Name safe for using as a file name like File_Name. I\'m sure there is a cocoa

7条回答
  •  离开以前
    2021-02-01 02:28

    According to wikipedia, the most common characters that should be excluded from filenames are:

    /\?%*|"<>

    http://en.wikipedia.org/wiki/Filename

    Given that, and since the invertedSet operation in the alternate solution can be intensive, to me the below is a cleaner approach:

    NSCharacterSet *invalidFsChars = [NSCharacterSet characterSetWithCharactersInString:@"/\\?%*|\"<>"];
    NSString *scrubbed = [originalStr stringByTrimmingCharactersInSet:invalidFsChars];
    

    This way you can still allow filenames that have dash, etc.

提交回复
热议问题