路径解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#import "FyQuestion.h"

@implementation FyQuestion

// 判断是绝对路径: 以 /开头的就是绝对路径
// -->./ios/day1/
// -->/oc/day
- (BOOL)isAbsolutePath:(NSString *)path {
//直接利用判断开头的这样的方法 harPrefix
return [path hasPrefix:@"/"];
}

// 取得路径path最后的部分 /home/xuanmac ---> xuanmac, /home/xuanmac/1.txt--->1.txt
- (NSString *)lastPathComponent:(NSString *)path {
//第一步,判断是否末尾存在/
int flag = 0;
NSString * subStr = nil;
if ([path hasSuffix:@"/"]) {
//截取字符方法,subStringToIndex
subStr = [path substringToIndex:[path length] - 1];
flag = 1;
}

//利用查找字符串方法,option 是倒序查找
//查找方法,返回的是一个范围
NSRange range = {0,0};
if (flag) {
//NSBackwardsSearch 倒序查找的指令
range = [subStr rangeOfString:@"/" options:NSBackwardsSearch];
//substringFromIndex 从指定位置到字符串结尾,包括指定位置
return [subStr substringFromIndex:range.location + 1];
} else {
range = [path rangeOfString:@"/" options:NSBackwardsSearch];
return [path substringFromIndex:range.location + 1];
}
}

// 删除路径path最后的部分 /home/xuanmac/ ---> /home, /home/xuanmac/1.txt--->/home/xuanmac
- (NSString *)stringByDeletingLastPathComponent:(NSString *)path {

NSString * subStr = nil;
//判断path末尾是不是以/结尾
if ([path hasSuffix:@"/"]) {
subStr = [path substringToIndex:path.length - 1];
} else {
subStr = path;
}

//倒序查找
NSRange range = [subStr rangeOfString:@"/" options:NSBackwardsSearch];

NSString * str = [subStr substringToIndex:range.location];

return [NSString stringWithString:str];

}
// 在路径上追加路径/home/xuanmac/ 和 demo/abc-->/home/xuanmac/demo/abc
- (NSString *)stringByAppendingPathComponent:(NSString *)path withPath:(NSString *)subPath {
NSMutableString * mStr = [NSMutableString stringWithString:path];
//判断是否是以/结尾
if ([mStr hasSuffix:@"/"]) {
if ([subPath hasPrefix:@"/"]) {
[mStr appendString:[subPath substringFromIndex:1]];
} else {
[mStr appendString:subPath];
}

} else {
if ([subPath hasPrefix:@"/"]) {
[mStr appendFormat:@"/%@",[subPath substringFromIndex:1]];
} else {
[mStr appendFormat:@"/%@",subPath];
}
}

return [NSString stringWithString:mStr];
}

// 取得文件的扩展名/home/xuanmac/1.txt --> txt
- (NSString *)pathExtension:(NSString *)path {
#if 1
//pathExtension 获取路径拓展名
NSString * str = path.pathExtension;
return [NSString stringWithString:str];
#else
NSRange range = [path rangeOfString:@"." options:NSBackwardsSearch];
NSString * str = [path substringFromIndex:range.location + 1];
return [NSString stringWithString:str];
#endif
}

// 删除文件扩展名/home/xuanmac/1.txt --> /home/xuanmac/1
- (NSString *)stringByDeletingPathExtension:(NSString *)path {
NSRange range = [path rangeOfString:@"." options:NSBackwardsSearch];

NSString * str = [path substringToIndex:range.location];
return [NSString stringWithString:str];
}
// 去除里面多余的 /路径 Duplicate重复 Slash表示斜线
// path = @"///home//xuanmac/ios/oc/day1/"; 返回@"/home/xuanmac/ios/oc/day1"
- (NSString *) removeDuplicateSlash:(NSString *)path {
#if 1
//看到//我们就要处理掉一个,知道在path没有// NSNotFound
//path 转换成可变字符串
NSMutableString * mStr = [NSMutableString stringWithString:path];

while (1) {
NSRange range = [mStr rangeOfString:@"//"];
//判断是否还存在,如果不存在@"//",跳出循环
if (NSNotFound == range.location) {
break;
}
//每次都删除//当中的一个
[mStr deleteCharactersInRange:NSMakeRange(range.location, 1)];
}

//处理完//之后,我们要处理路径末尾的/
if([mStr hasSuffix:@"/"])
{
[mStr deleteCharactersInRange:NSMakeRange(mStr.length - 1, 1)];
}

return [NSString stringWithString:mStr];
#else
NSArray * array = [path componentsSeparatedByString:@"/"];
NSMutableArray * mArray = [NSMutableArray arrayWithArray:array];
[mArray removeObject:@""];

return [mArray componentsJoinedByString:@"/"];//?????
#endif
}
//-获取路径中的所有目录名---->/home/xuanmac/ios/oc --->home xuanmac ios oc
- (NSArray *)pathComponents:(NSString*)path {
return [path componentsSeparatedByString:@"/"];
}

+ (void)test {
FyQuestion * test = [[FyQuestion alloc] init];
NSLog(@"第一题");
NSLog(@"%hhd",[test isAbsolutePath:@"./ios/day1/"]);
NSLog(@"%hhd",[test isAbsolutePath:@"/ios/day1/"]);

NSLog(@"第二题");
NSString * str1 = [test lastPathComponent:@"/ios/day1/"];
NSLog(@"%@",str1);
NSString * str2 = [test lastPathComponent:@"/ios/day1/1.txt"];
NSLog(@"%@",str2);

NSLog(@"第三题");
NSString * str3 = [test stringByDeletingLastPathComponent:@"/ios/day1/"];
NSLog(@"%@",str3);
NSString * str4 = [test stringByDeletingLastPathComponent:@"/ios/day1/1.txt"];
NSLog(@"%@",str4);

NSLog(@"第四题");
NSString * str5 = [test stringByAppendingPathComponent:@"/ios/day1/" withPath:@"1.txt"];
NSLog(@"%@",str5);
NSString * str6 = [test stringByAppendingPathComponent:@"/ios/day1" withPath:@"/test/1.txt"];
NSLog(@"%@",str6);

NSLog(@"第五题");
NSLog(@"%@",[test pathExtension:@"/test/1.txt"]);

NSLog(@"第六题");
NSLog(@"%@",[test stringByDeletingPathExtension:@"/test/1.txt"]);
NSLog(@"第七题");
NSLog(@"%@",[test removeDuplicateSlash:@"/////////fdjkasj//fdhk///fdjsalfkjs///"]);
NSLog(@"第八题");

NSLog(@"第备用题");
}


@end