To get the file name of the url

I have source code to get url file name

eg:

http://www.google.com/a.pdf

I hope to get a.pdf

because the way to join the two NSStrings that I can get is "appendString", which is just for adding a line on the right side, so I planned to tag each char on the right side of the line <http://www.google.com/ a.pdf 'when it reaches the char' / 'value, stop checking, return the line fdp.a, after which I will change fdp.a to a.pdf

source codes below

-(NSMutableString *) getSubStringAfterH : originalString:(NSString *)s0 { NSInteger i,l; l=[s0 length]; NSMutableString *h=[[NSMutableString alloc] init]; NSMutableString *ttt=[[NSMutableString alloc] init ]; for(i=l-1;i>=0;i--) //check each char one by one from the right side of string 'http://www.google.com/a.pdf', when it reach at the char '/', stop { ttt=[s0 substringWithRange:NSMakeRange(i, 1)]; if([ttt isEqualToString:@"/"]) { break; } else { [h appendString:ttt]; } } [ttt release]; NSMutableString *h1=[[[NSMutableString alloc] initWithFormat:@""] autorelease]; for (i=[h length]-1;i>=0;i--) { NSMutableString *t1=[[NSMutableString alloc] init ]; t1=[h substringWithRange:NSMakeRange(i, 1)]; [h1 appendString:t1]; [t1 release]; } [h release]; return h1; } 

h1 can reuse the coorect a.pdf line, but if it returns to the codes where it was called, after a while the system reports "double free *** set a breakpoint in malloc_error_break for debugging

I checked for a long time and realized that if I delete the code

ttt = [s0 substringWithRange: NSMakeRange (i, 1)];

everything will be OK (of course, getSubStringAfterH cannot return the result of the root process that I expected.), error messages are not reported.

I have been trying to fix the error for several hours, but still do not know.

Welcome any comment

Thanks interdev

+45
iphone
Mar 14
source share
6 answers

Try the following:

Edit : from comment comment

 NSString *url = @"http://www.google.com/a.pdf"; NSArray *parts = [url componentsSeparatedByString:@"/"]; NSString *filename = [parts lastObject]; 
+62
Mar 14 '10 at 1:38
source share

The following line does the job if the url is NSString:

 NSString *filename = [url lastPathComponent]; 

If url is NSURL, then the following task is performed:

 NSString *filename = [[url path] lastPathComponent]; 
+175
May 19 '10 at 11:33
source share

I think that if you already have an NSURL object, there is a lastPathComponent method available from iOS 4 onwards.

 NSURL *url = [NSURL URLWithString:@"http://www.google.com/a.pdf"]; NSString *filename = [url lastPathComponent]; 
+9
Apr 04 2018-11-11T00:
source share

This is more of a mistake and is intended to get a localized name in the url.

 NSString *localizedName = nil; [url getResourceValue:&localizedName forKey:NSURLLocalizedNameKey error:NULL]; 
+3
Feb 27 2018-12-12T00:
source share

I have not tried this yet, but it looks like you are trying to do it with difficulty. IPhone libraries have an NSURL class, and I suppose you could just do:

 NSString *url = [NSURL URLWithString:@"http://www.google.com/a.pdf"]; NSString *path = [url path]; 

Definitely look for an inline function. Libraries have a much larger number of tests and will handle boundary cases better than anything you or I will write in an hour or two (generally speaking).

+1
Mar 14 '10 at 0:09
source share

Swift 3

Say your url is http://www.google.com/a.pdf

  let filename = url.lastPathComponent \\filename = "a.pdf" 
+1
Apr 18 '17 at 8:06 on
source share



All Articles