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
- (BOOL)isAbsolutePath:(NSString *)path { return [path hasPrefix:@"/"]; }
- (NSString *)lastPathComponent:(NSString *)path { int flag = 0; NSString * subStr = nil; if ([path hasSuffix:@"/"]) { subStr = [path substringToIndex:[path length] - 1]; flag = 1; } NSRange range = {0,0}; if (flag) { range = [subStr rangeOfString:@"/" options:NSBackwardsSearch]; return [subStr substringFromIndex:range.location + 1]; } else { range = [path rangeOfString:@"/" options:NSBackwardsSearch]; return [path substringFromIndex:range.location + 1]; } }
- (NSString *)stringByDeletingLastPathComponent:(NSString *)path { NSString * subStr = nil; 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]; }
- (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]; }
- (NSString *)pathExtension:(NSString *)path { #if 1 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 }
- (NSString *)stringByDeletingPathExtension:(NSString *)path { NSRange range = [path rangeOfString:@"." options:NSBackwardsSearch]; NSString * str = [path substringToIndex:range.location]; return [NSString stringWithString:str]; }
- (NSString *) removeDuplicateSlash:(NSString *)path { #if 1 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 }
- (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
|