iPhone-获取网络数据或者路径的文件名
iPhone中,在网络中的数据流中提取链接中的文件名称时,有很多方法,这里总结一些。
方法一:最直接。
1 NSString * urlString = @"http://www.baidu.com/img/baidu_logo_fqj_10.gif";
2 NSString *fileName = [urlString lastPathComponent];
3 NSLog(@"%@",fileName);
2 NSString *fileName = [urlString lastPathComponent];
3 NSLog(@"%@",fileName);
方法二:根据字符或者时字符串分割。
Object -C
1 NSString *link = @"http://www.baidu.com/img/baidu_logo_fqj_10.gif";
2
3 NSString *filename = [[NSString alloc] init];
4 NSArray *SeparatedArray = [[NSArray alloc]init];
5 SeparatedArray =[link componentsSeparatedByString:@"/"];
6 filename = [SeparatedArray lastObject];
7
8
9 NSLog(@"%@",SeparatedArray);
10 NSLog(@"%@",filename);
11
12 [filename release];
2
3 NSString *filename = [[NSString alloc] init];
4 NSArray *SeparatedArray = [[NSArray alloc]init];
5 SeparatedArray =[link componentsSeparatedByString:@"/"];
6 filename = [SeparatedArray lastObject];
7
8
9 NSLog(@"%@",SeparatedArray);
10 NSLog(@"%@",filename);
11
12 [filename release];
方法三:将链接看成路径。
1 NSString * urlString = @"http://www.baidu.com/img/baidu_logo_fqj_10.gif";
2 NSArray *urlCom = [[NSArray alloc]initWithArray:[url pathComponents]];
3
4 NSLog(@"%@",[urlCom lastObject]);
5
6 [urlCom release];
2 NSArray *urlCom = [[NSArray alloc]initWithArray:[url pathComponents]];
3
4 NSLog(@"%@",[urlCom lastObject]);
5
6 [urlCom release];
方法四:NSRange.它在截取二进制文件的时候十分方便。
1 NSString * urlString = @"http://www.baidu.com/img/baidu_logo_fqj_10.gif";
2 NSString * fileName;
3
4 NSRange range = [urlString rangeOfString:@"/" options:NSBackwardsSearch];
5
6 if (range.location != NSNotFound)
7 {
8 fileName = [urlString substringFromIndex:range.location+1];
9
10 if([[fileName lowercaseString]hasSuffix:@".gif"])
11 {
12 NSLog(@"%@",fileName);
13 }
14 else
15 {
16 }
17 }
18 else
19 {
20 return;
21 }
22
2 NSString * fileName;
3
4 NSRange range = [urlString rangeOfString:@"/" options:NSBackwardsSearch];
5
6 if (range.location != NSNotFound)
7 {
8 fileName = [urlString substringFromIndex:range.location+1];
9
10 if([[fileName lowercaseString]hasSuffix:@".gif"])
11 {
12 NSLog(@"%@",fileName);
13 }
14 else
15 {
16 }
17 }
18 else
19 {
20 return;
21 }
22