用于获取URL的文件名的函数 [英] function to get the file name of an URL

查看:259
本文介绍了用于获取URL的文件名的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些源代码来获取网址的文件名

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

例如:

< a href =http://www.google.com/a.pdf =noreferrer> http://www.google.com/a.pdf

我希望得到a.pdf

I hope to get a.pdf

因为我可以得到的加入2个NSStrings的方法是'appendString',它只用于在右边添加一个字符串所以我计划从字符串' http:// www。的右侧逐个检查每个字符。 google.com/a.pdf ',当它到达char'/'时,停止检查,返回字符串fdp.a,之后我将fdp.a更改为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

源代码低于

-(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可以重新输出coorect字符串a.pdf,但如果它返回到调用它的代码,在一段时间之后系统报告
'双重免费
***在malloc_error_break中设置断点以调试'

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)];

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

一切都会好的(当然getSubStringAfterH无法返回我预期的正确结果。),没有报告错误。

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

我尝试修复错误a几个小时,但仍然没有线索。

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

欢迎任何评论

谢谢
interdev

Thanks interdev

推荐答案

试试这个:

编辑:来自评论

NSString *url = @"http://www.google.com/a.pdf";
NSArray *parts = [url componentsSeparatedByString:@"/"];
NSString *filename = [parts lastObject];

这篇关于用于获取URL的文件名的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆