[myString lastPathComponent]
将获得文件名。
[myString pathExtension]
将获得扩展名。
要获得文件名的后缀,我认为您必须滚动自己的解析。它总是在最后一个破折号之后和扩展之前的字符串吗?
如果是这样,这里有一个想法:
- (NSString *)lastLittleBitOfTheFilenameFrom:(NSString *)filename {
NSInteger fnStart = [filename rangeOfString:@"-" options:NSBackwardsSearch].location + 1;
NSInteger fnEnd = [filename rangeOfString:@"." options:NSBackwardsSearch].location;
// might need some error checks here depending on what you expect in the original url
NSInteger length = fnEnd - fnStart;
return [filename substringWithRange:NSMakeRange(fnStart, length)];
}
或者,感谢@Chuck ...
// even more sensitive to unexpected input, but nice and tiny ...
- (NSString *)lastLittleBitOfTheFilenameFrom:(NSString *)filename {
NSString *nameExt = [[filename componentsSeparatedByString:@"-"] lastObject];
return [[nameExt componentsSeparatedByString:@"."] objectAtIndex:0];
}