Objective c 如何获取两个字符串的第一个不同范围
我有两个字符串,如下所示:Objective c 如何获取两个字符串的第一个不同范围,objective-c,nsstring,Objective C,Nsstring,我有两个字符串,如下所示: NSString *path1 = @"/Users/user/Desktop/AAA/BBB/1.txt"; NSString *path2 = @"/Users/user/Document/test/AAA/CCC/2.txt"; 现在,我想为两个字符串获取第一个不同的字符串:path1的“Desktop”和path2的“Document/test”。如果有两个以上的NSString呢 有什么算法可以实现这一点吗?试试这样的方法 -(NSString *)fin
NSString *path1 = @"/Users/user/Desktop/AAA/BBB/1.txt";
NSString *path2 = @"/Users/user/Document/test/AAA/CCC/2.txt";
现在,我想为两个字符串获取第一个不同的字符串:path1的“Desktop”和path2的“Document/test”。如果有两个以上的NSString呢
有什么算法可以实现这一点吗?试试这样的方法
-(NSString *)findDiferentIn:(NSArray *)first By:(NSArray *)second{
for (int i = 0; i < [first count]; i++) {
if (![second objectAtIndex:i]) {
return [first objectAtIndex:i];
}else{
if (![[first objectAtIndex:i] isEqualToString:[second objectAtIndex:i]]) {
return [first objectAtIndex:i];
}
}
}
return @"";
}
NSString *path1 = @"/Users/user/Desktop/AAA/BBB/1.txt";
NSString *path2 = @"/Users/user/Document/test/AAA/CCC/2.txt";
NSLog(@"diferent in first string: %@", [self findDiferentIn:[path1 componentsSeparatedByString:@"/"] By:[path2 componentsSeparatedByString:@"/"]]);
NSLog(@"diferent in second string: %@", [self findDiferentIn:[path2 componentsSeparatedByString:@"/"] By:[path1 componentsSeparatedByString:@"/"]]);