从下面不同的urls,我只需要文件名
URL 1= uploads/4/jobs/2017-02-15-05-17-05pmThe_Wolf_of_Wall_Street_-_Jordan_Belfort.pdf URL 2= uploads/4/jobs/2017-02-15-05-17-05ammaster.jpg
我需要的是
Result1 = The_Wolf_of_Wall_Street_-_Jordan_Belfort
Result2 = master发布于 2017-02-15 07:20:21
试着用这个:
let name = "uploads/4/jobs/2017-02-15-05-17-05pmThe_Wolf_of_Wall_Street_-_Jordan_Belfort.pdf"
var names = name.components(separatedBy: "/")
let lastComponent = names[names.count - 1]
var nameOfFile:String = ""
let pm = lastComponent.components(separatedBy: "pm")
let am = lastComponent.components(separatedBy: "am")
if(pm.count > 1){
nameOfFile = pm[1]
} else if(am.count > 1){
nameOfFile = am[1]
}
print(nameOfFile)发布于 2017-02-15 07:26:18
如果您的url将始终遵循开始命名格式。
let urlStr = "uploads/4/jobs/2017-02-15-05-17-05pmThe_Wolf_of_Wall_Street_-_Jordan_Belfort.pdf"
let indexStart = urlStr.index(urlStr.startIndex, offsetBy: +36)
let str = urlStr.substring(from: indexStart )
let indexEnd = str.index(str.endIndex, offsetBy: -4)
let name = str.substring(to: indexEnd )发布于 2017-02-15 07:24:50
您可以尝试下面的目标-C代码
NSString* string = @"uploads/4/jobs/2017-02-15-05-17-05pmThe_Wolf_of_Wall_Street_-_Jordan_Belfort.pdf";
NSArray* array = [string componentsSeparatedByString:@"/"];
NSString* lastElement = [array lastObject];
NSArray* finalArrayForPM = [lastElement componentsSeparatedByString:@"pm"];
NSArray* finalArrayForAM = [lastElement componentsSeparatedByString:@"am"];
if (finalArrayForPM.count > 1) {
NSString* fileName = [[finalArrayForPM lastObject] stringByDeletingPathExtension];
NSLog(@"The fileName is %@",fileName);
} else if (finalArrayForAM.count > 1) {
NSString* fileName = [[finalArrayForAM lastObject] stringByDeletingPathExtension];
NSLog(@"The fileName is %@",fileName);
}希望这能有所帮助。
https://stackoverflow.com/questions/42242763
复制相似问题