在扩展名之前为文件名添加后缀

Posted

技术标签:

【中文标题】在扩展名之前为文件名添加后缀【英文标题】:Add suffix to file name before extension 【发布时间】:2010-10-17 15:10:45 【问题描述】:

在 Objective-C 中,的最简单方法是什么?

示例

原名:image.png 后缀:~ipad 结果:image~ipad.png

【问题讨论】:

【参考方案1】:

NSString 有一大堆与路径相关的方法:

NSString * appendSuffixToPath(NSString * path, NSString * suffix) 
    NSString * containingFolder = [path stringByDeletingLastPathComponent];
    NSString * fullFileName = [path lastPathComponent];
    NSString * fileExtension = [fullFileName pathExtension];
    NSString * fileName = [fullFileName stringByDeletingPathExtension];
    NSString * newFileName = [fileName stringByAppendingString:suffix];
    NSString * newFullFileName = [newFileName stringByAppendingPathExtension:fileExtension];

    return [containingFolder stringByAppendingPathComponent:newFullFileName];

(是的,你可以用更少的变量来做到这一点,但我想弄清楚每种方法的作用)

【讨论】:

谢谢戴夫!我怀疑 Obj-C 中有类似 stringByDeletingPathExtension 的东西。【参考方案2】:

知道它很旧,但认为有人可能会喜欢这个 Swift 解决方案

extension String 
func appendSuffixBeforeExtension(suffix: String) -> String 
        let regex = NSRegularExpression(pattern: "(\\.\\w+$)", options: nil, error: nil)
        return regex!.stringByReplacingMatchesInString(self, options: nil, range: NSMakeRange(0, count(self)), withTemplate: "\(suffix)$1")
    

那就这样称呼吧

let mytxt = "/somepath/file.jpg"
let retina = mytxt.appendSuffixBeforeExtension("@x2")

【讨论】:

【参考方案3】:

使用URL 方法:

let suffixedFileURL = fileURL
    .deletingLastPathComponent()
    .appendingPathComponent(
        fileURL
            .deletingPathExtension()
            .lastPathComponent
            .appending("suffix")
    )
    .appendingPathExtension(fileURL.pathExtension)

【讨论】:

【参考方案4】:

上述解决方案不适用于 ../filename.pvr.ccz 等文件名

所以我对它进行了一些调整以考虑多个文件扩展名:

-(NSString *)appendSuffixToPath:(NSString *)pPath withSuffix:(NSString *)pSuffix 
    NSString * containingFolder = [pPath stringByDeletingLastPathComponent];
    NSString * fullFileName = [pPath lastPathComponent];

    NSMutableArray *extensions = [[NSMutableArray alloc] init];
    NSString * fileExtension = [fullFileName pathExtension];
    [extensions addObject:fileExtension];

    NSString * fileName = [fullFileName stringByDeletingPathExtension];
    while([fileName pathExtension].length > 0)
        fileExtension = [fileName pathExtension];
        [extensions addObject:fileExtension];
        fileName = [fileName stringByDeletingPathExtension];
    

    NSString * newFileName = [fileName stringByAppendingString:pSuffix];

    NSString * newFullFileName = newFileName;
    while ([extensions count] > 0) 
        newFullFileName = [newFullFileName stringByAppendingPathExtension:[extensions objectAtIndex:((int)[extensions count] - 1)]];
        [extensions removeObjectAtIndex:((int)[extensions count] - 1)];
    

    NSString *result = [containingFolder stringByAppendingPathComponent:newFullFileName];
    return result;

【讨论】:

以上是关于在扩展名之前为文件名添加后缀的主要内容,如果未能解决你的问题,请参考以下文章

文件如何批量添加后缀名?

怎么隐藏文件后缀名?

文件后缀名

怎样才能把文件的后缀名隐藏?

识别表空间(非扩展)使用率并自动生成添加数据文件脚本

怎么去掉文件后面的后缀名