函数以获取URL的文件名

时间:2023-01-26 15:06:48

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

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

for example:

例如:

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

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

I hope to get a.pdf

我希望得到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

因为我可以加入2 nsstring是“appendString”只在右边添加一个字符串,所以我打算检查每个一个接一个的字符从字符串的右边“http://www.google.com/a.pdf”,当它到达字符' / ',自民党停止检查,返回字符串。a,之后我换自民党。a 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];
     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'

h1可以旋转coorect字符串a。pdf,但是如果它返回到被调用的代码,过一会儿,系统报告'double free ***在malloc_error_break中设置断点以进行调试'

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

我检查了很长时间,发现如果我删除了代码

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

到达目标时间=[s0 substringWithRange:NSMakeRange(我,1)];

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

一切都会好起来(当然getSubStringAfterH不能返回我期望的相关结果),没有错误报告。

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

我试着把这个bug修复几个小时,但还是毫无头绪。

Welcome any comment

欢迎任何评论

Thanks interdev

由于interdev

6 个解决方案

#1


62  

Try this:

试试这个:

Edit: from blow comment

编辑:从打击发表评论

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

#2


176  

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

如果url是一个NSString,下面这行就可以完成:

NSString *filename = [url lastPathComponent];

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

如果url是NSURL,那么下面的操作是:

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

#3


9  

I think if you have already had the NSURL object, there is lastPathComponent method available from the iOS 4 onwards.

我认为如果您已经有了NSURL对象,那么从ios4开始就有了lastPathComponent方法。

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

#4


3  

This is more error free and meant for getting the localized name in the URL.

这是更多的错误,意味着在URL中获取本地化的名称。

NSString *localizedName = nil;
[url getResourceValue:&localizedName forKey:NSURLLocalizedNameKey error:NULL];

#5


1  

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:

我还没试过这个,但看起来你可能在用困难的方法。iPhone库有NSURL类,我想您可以简单地做:

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).

一定要寻找内建函数。这些库有更多的测试,处理边缘情况比您或我在一两个小时内编写的任何东西都要好(一般来说)。

#6


1  

Swift 3

斯威夫特3

Let's say that your url is http://www.google.com/a.pdf

假设你的url是http://www.google.com/a.pdf

    let filename = url.lastPathComponent

    \\filename = "a.pdf"

#1


62  

Try this:

试试这个:

Edit: from blow comment

编辑:从打击发表评论

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

#2


176  

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

如果url是一个NSString,下面这行就可以完成:

NSString *filename = [url lastPathComponent];

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

如果url是NSURL,那么下面的操作是:

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

#3


9  

I think if you have already had the NSURL object, there is lastPathComponent method available from the iOS 4 onwards.

我认为如果您已经有了NSURL对象,那么从ios4开始就有了lastPathComponent方法。

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

#4


3  

This is more error free and meant for getting the localized name in the URL.

这是更多的错误,意味着在URL中获取本地化的名称。

NSString *localizedName = nil;
[url getResourceValue:&localizedName forKey:NSURLLocalizedNameKey error:NULL];

#5


1  

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:

我还没试过这个,但看起来你可能在用困难的方法。iPhone库有NSURL类,我想您可以简单地做:

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).

一定要寻找内建函数。这些库有更多的测试,处理边缘情况比您或我在一两个小时内编写的任何东西都要好(一般来说)。

#6


1  

Swift 3

斯威夫特3

Let's say that your url is http://www.google.com/a.pdf

假设你的url是http://www.google.com/a.pdf

    let filename = url.lastPathComponent

    \\filename = "a.pdf"