1.NSString与路径
// 其实就是判断是否以/开头
// NSString *str = @"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
NSString *str = @"Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
if ([str isAbsolutePath]) {
NSLog(@"是绝对路径");
}else
{
NSLog(@"不是绝对路径");
}
- -(NSString *)lastPathComponent;
// 截取最后一个/后面的内容
NSString *str = @"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
NSString *component = [str lastPathComponent];
NSLog(@"component = %@", component);
- -(NSString *)stringByDeletingLastPathComponent;
// 其实就是上次最后一个/和之后的内容
NSString *str = @"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
NSString *newStr = [str stringByDeletingLastPathComponent];
NSLog(@"newStr = %@", newStr);
- -(NSString )stringByAppendingPathComponent:(NSString )str;
- 在路径的后面拼接一个目录 (也可以使用stringByAppendingString:或者stringByAppendingFormat:拼接字符串内容)
// 其实就是在最后面加上/和要拼接得内容
// 注意会判断后面有没有/有就不添加了, 没有就添加, 并且如果有多个会替换为1个
// NSString *str = @"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt";
NSString *str = @"/Users/WJim/Desktop/Objective-CJiChuXueXi/Day7/cwj.txt/";
NSString *newStr = [str stringByAppendingPathComponent:@"lnj"];
NSLog(@"newStr = %@", newStr);
2.NSString与文件拓展名
- -(NSString *)pathExtension;
// 其实就是从最后面开始截取.之后的内容
// NSString *str = @"cwj.txt";
NSString *str = @"abc.cwj.txt";
NSString *extension = [str pathExtension];
NSLog(@"extension = %@", extension);
- -(NSString *)stringByDeletingPathExtension;
// 其实就是上次从最后面开始.之后的内容
// NSString *str = @"cwj.txt";
NSString *str = @"abc.cwj.txt";
NSString *newStr = [str stringByDeletingPathExtension];
NSLog(@"newStr = %@", newStr);
- -(NSString )stringByAppendingPathExtension:(NSString )str;
// 其实就是在最后面拼接上.和指定的内容
NSString *str = @"cwj";
NSString *newStr = [str stringByAppendingPathExtension:@"txt"];
NSLog(@"newStr = %@", newStr);