我想从文件名中删除特殊字符。请找到以下代码。我有删除的问题。从文件名中,我的代码也删除了.in扩展名。
Ex filename = atf.bac.xlsx
Expected result: atfbac.xlsx
Current Behavior:atfbacxlsx
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@".#%;:?,/|\[]{}@$%^*"];
fileName = [[fileName componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
答案 0 :(得分:6)
您必须将字符串视为路径或网址中的文件名
添加文件扩展名。
NSString *fileName = @"atf.bac.xlsx";
NSString *extension = fileName.pathExtension;
NSString *baseName = [fileName stringByDeletingPathExtension];
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@".#%;:?,/|\[]{}@$%^*"];
fileName = [[[baseName componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""] stringByAppendingPathExtension:extension];
NSLog(@"%@",fileName);