Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何获取两个字符串的第一个不同范围_Objective C_Nsstring - Fatal编程技术网

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:@"/"]]);