zoukankan      html  css  js  c++  java
  • lastPathComponent的功能

    下面是官方的说明:

    源字符串 

      --->     结果字符串
    “/tmp/scratch.tiff”   --->     “scratch.tiff”
    “/tmp/scratch”   --->     “scratch”
    “/tmp/”   --->     “tmp”
    “scratch”   --->     “scratch”
    “/”   --->     “/”


    获取网络数据或者路径的文件名以及后缀
    2012年02月10日 星期五 14:38
    // 从路径中获得完整的文件名(带后缀)     
    exestr = [filePath lastPathComponent]; 
    NSLog(@"%@",exestr); 
    // 获得文件名(不带后缀) 
    exestr = [exestr stringByDeletingPathExtension];     
    NSLog(@"%@",exestr); 
     
    // 获得文件的扩展类型(不带'.') 
    exestr = [filePath pathExtension]; 
    NSLog(@"%@",exestr); 

    NSString *path = @"~/textFile.txt"; 
    NSString *pathExtension = [path pathExtension]; 
    pathExtension这个字符串的值将是“txt”。句点将被去掉了。如果没有句点指明扩展名,将返回一个空串。如果文件不存在,也将返回空串  


    [[imageName componentsSeparatedByString:@"."] objectAtIndex:0] 
    用.分开, objectAtIndex:0为文件名, objectAtIndex:1为后缀  


    iPhone-获取网络数据或者路径的文件名 

    iPhone中,在网络中的数据流中提取链接中的文件名称时,有很多方法,这里总结一些。 
    方法一:最直接。 
    NSString * urlString = @”http://www.baidu.com/img/baidu_logo_fqj_10.gif”; 
    NSString *fileName = [urlString lastPathComponent]; 
    NSLog(@”%@”,fileName); 
     
    方法二:根据字符或者时字符串分割。 
    NSString *link = @”http://www.baidu.com/img/baidu_logo_fqj_10.gif”; 
    NSString *filename = [[NSString alloc] init]; 
    NSArray *SeparatedArray = [[NSArray alloc]init]; 
    SeparatedArray =[link componentsSeparatedByString:@"/"]; 
    filename = [SeparatedArray lastObject]; 
    NSLog(@”%@”,SeparatedArray); 
    NSLog(@”%@”,filename); 
    [filename release]; 

    方法三:将链接看成路径。 
    NSString * urlString = @”http://www.baidu.com/img/baidu_logo_fqj_10.gif”; 
    NSArray *urlCom = [[NSArray alloc]initWithArray:[url pathComponents]]; 
    NSLog(@”%@”,[urlCom lastObject]); 
    [urlCom release]; 

    方法四:NSRange.它在截取二进制文件的时候十分方便。 
    NSString * urlString = @”http://www.baidu.com/img/baidu_logo_fqj_10.gif”; 
    NSString * fileName; 
    NSRange range = [urlString rangeOfString:@"/" options:NSBackwardsSearch]; 
    if (range.location != NSNotFound) 

    fileName = [urlString substringFromIndex:range.location+1]; 
    if([[fileName lowercaseString]hasSuffix:@”.gif”]) 

    NSLog(@”%@”,fileName); 


    else 

    return; 
    }
     
     
     
  • 相关阅读:
    【2019.7.26 NOIP模拟赛 T1】数字查找(figure)(数学)
    【2019.7.25 NOIP模拟赛 T1】变换(change)(思维+大分类讨论)
    简析平衡树(四)——FHQ Treap
    【BZOJ3529】[SDOI2014] 数表(莫比乌斯反演)
    【洛谷1829】 [国家集训队] Crash的数字表格(重拾莫比乌斯反演)
    【PE512】Sums of totients of powers(欧拉函数)
    【CFGym102059G】Fascination Street(思维DP)
    【CF438D】The Child and Sequence(线段树)
    【2019.7.16 NOIP模拟赛 T2】折叠(fold)(动态规划)
    【UVA1303】Wall(凸包)
  • 原文地址:https://www.cnblogs.com/zhangdashao/p/4554496.html
Copyright © 2011-2022 走看看