1 NSString * urlString = @"http://www.baidu.com/img/baidu_logo_fqj_10.gif"; 2 //方法一:最直接 3 NSString * fileName = [urlString lastPathComponent]; 4 NSLog(@"fileName==%@",fileName); 5 //方法二:根据字符或者字符串分割 6 NSString * fileName2 = [[NSString alloc] init]; 7 NSArray * separatedArray = [[NSArray alloc] init]; 8 separatedArray = [urlString componentsSeparatedByString:@"/"]; 9 fileName2 = [separatedArray lastObject]; 10 NSLog(@"separatedArray==%@",separatedArray); 11 NSLog(@"fileName2==%@",fileName2); 12 //方法三:将链接看成是路劲 13 NSArray * urlCom = [[NSArray alloc] initWithArray:[urlString pathComponents]]; 14 NSLog(@"[urlCom lastObject]===%@",[urlCom lastObject]); 15 //方法四:NSRange.它在截取二进制文件的时候十分方便 16 NSString * fileName3; 17 NSRange range = [urlString rangeOfString:@"/" options:NSBackwardsSearch];//NSBackwardsSearch从字符串末尾开始检索 18 if (range.location != NSNotFound) { 19 fileName3 = [urlString substringFromIndex:range.location+1]; 20 if ([[fileName3 lowercaseString] hasSuffix:@".gif"]) {//lowercaseString 改变字母为小写 uppercaseString改变字母切大写 capitalizedString 首字母大些转换 · 21 NSLog(@"fileName3====%@",fileName3); 22 }else{ 23 24 } 25 }else{ 26 27 }