tags:

views:

363

answers:

3

Hello everyone

I have some source code to get the file name of an url

for example:

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

I hope to get a.pdf

because the way to join 2 NSStrings I can get is 'appendString' which only for adding a string at right side, so I planned to 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 the checking, return string fdp.a , after that I change fdp.a to a.pdf

source codes are 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];

    //below are to change the sequence of char in h
    // txt.edcba ->  abcde.txt

    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 reuturn the coorect string a.pdf, but if it returns to the codes where it was called, after a while system reports 'double free * set a breakpoint in malloc_error_break to debug'

I checked a long time and foudn that if I removed the code

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

everything will be Ok (of course getSubStringAfterH can not returns the corrent result I expected.), no error reported.

I try to fix the bug a few hours, but still no clue.

Welcome any comment

Thanks interdev

+2  A: 

I haven't tried this yet, but it seems like you might be trying to do this the hard way. The iPhone libraries have the NSURL class, and I imagine that you could simply do:

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

Definitely look for a built in function. The libraries have far more testing and will handle the edge cases better than anything you or I will write in an hour or two (generally speaking).

marcc
ThanksBut it returns file name with path rather than filenamefor example/download/10925221/11263992/1/mp4/39/71/1268527574311_583/03002001004B9B86E1927C01CCF99112E21A1F-E815-DA28-CB74-4CA536C67D87.mp4rather than 03002001004B9B86E1927C01CCF99112E21A1F-E815-DA28-CB74-4CA536C67D87.mp4
A: 

Try this:

NSString *url = @"http://www.google.com/a.pdf";
NSArray *parts = [url componentsSeparatedByString:@"/"];
NSString *filename = [parts objectAtIndex:[parts count]-1];
DyingCactus
NSString *filename = [parts lastObject];
thomax
@thomax: Thanks, that looks cleaner.
DyingCactus
+2  A: 

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

NSString *filename = [url lastPathComponent];

If url is a NSURL, then the following does the job:

NSString *filename = [[url path] lastPathComponent];

occamsrazor
Minor issue with this is that given @"http://www.google.com/" (slash at end), it returns @"www.google.com" which may not be what you want.
DyingCactus