Reputation: 70
From below different urls i need file name only
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
What I need is
Result1 = The_Wolf_of_Wall_Street_-_Jordan_Belfort
Result2 = master
Upvotes: 0
Views: 161
Reputation: 5098
You can try the below code for Objective-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);
}
Hope this helps.
Upvotes: 0
Reputation: 983
If your url will always follow start naming format.
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 )
Upvotes: 1
Reputation: 434
Try with this:
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)
Upvotes: 1