4

我有两个NSStrings,我想了解它们之间的差异。

让我举个例子:

NSString *pippo = @"pippo";
NSString *pippone = @"pippone";

我想在两个实例中获得不同的字符。我怎样才能做到这一点?

4

3 回答 3

2

尽管可以使用更简单的算法来处理几种特殊情况(例如后缀),但如果处理一般情况,您可以:

1/首先提取一个公共序列。除非您的字符串很长,否则最长的公共子序列算法将是您的选择(LCS,请参阅https://en.wikipedia.org/wiki/Longest_common_subsequence_problem)。

你可以在这里找到 LCS 的 obj-c 和 swift 实现:http: //jakubturek.pl/blog/2015/06/27/using-swifts-string-type-with-care/

2/ 从 LCS,很容易获得差异,例如参见https://en.wikipedia.org/wiki/Diff_utility。请参阅下面的 obj-C 实现(NSString 类别,假设我们有最长的CommonSubsequence:来自上面的链接):

- (NSArray *) lcsDiff:(NSString *)string
{
    NSString *lcs = [self longestCommonSubsequence:string];
    NSUInteger l1 = [self length];
    NSUInteger l2 = [string length];
    NSUInteger lc = [lcs length];
    NSUInteger idx1 = 0;
    NSUInteger idx2 = 0;
    NSUInteger idxc = 0;
    NSMutableString *s1 = [[NSMutableString alloc]initWithCapacity:l1];
    NSMutableString *s2 = [[NSMutableString alloc]initWithCapacity:l2];
    NSMutableArray *res = [NSMutableArray arrayWithCapacity:10];
    for (;;) {
        if (idxc >= lc) break;
        unichar c1 = [self characterAtIndex:idx1];
        unichar c2 = [string characterAtIndex:idx2];
        unichar cc = [lcs characterAtIndex:idxc];
        if ((c1==cc) && (c2 == cc)) {
            if ([s1 length] || [s2 length]) {
                NSArray *e = @[ s1, s2];
                [res addObject:e];
                s1 = [[NSMutableString alloc]initWithCapacity:l1];
                s2 = [[NSMutableString alloc]initWithCapacity:l1];
            }
            idx1++; idx2++; idxc++;
            continue;
        }
        if (c1 != cc) {
            [s1 appendString:[NSString stringWithCharacters:&c1 length:1]];
            idx1++;
        }
        if (c2 != cc) {
            [s2 appendString:[NSString stringWithCharacters:&c2 length:1]];
            idx2++;
        }
    }
    if (idx1<l1) {
        [s1 appendString:[self substringFromIndex:idx1]];
    }
    if (idx2<l2) {
        [s2 appendString:[string substringFromIndex:idx2]];
    }
    if ([s1 length] || [s2 length]) {
        NSArray *e = @[ s1, s2];
        [res addObject:e];
    }
    return res;
}

代码将为两个字符串之间的每个差异返回一个包含一个元素 (string1,string2) 的数组,例如 [@"abcXefY" lcsDiff:@"aZbcKef"] 将返回@[ @[ @"", @"Z"], @[ @"X", @"K"], @[ @"Y", @""]];

于 2015-10-03T00:29:10.983 回答
0

您可以使用一些正则表达式:

我做了一次这个函数,用你想要的字符串/模式替换另一个字符串中的字符串。

-(NSString *)replaceInString:(NSString *)chaine :(NSString *)pattern
             :(NSString *)template
{
NSMutableString *chaineMutable = [[NSMutableString alloc] initWithString:chaine];
NSRegularExpression *regex = [[NSRegularExpression alloc] init];

regex = [NSRegularExpression regularExpressionWithPattern:pattern
             options:NSRegularExpressionCaseInsensitive error:NULL];

[regex replaceMatchesInString:(NSMutableString *)chaineMutable
             options:NSMatchingReportProgress range:NSMakeRange(0, [chaine length])
             withTemplate:template];

NSString *returnedString = [[NSString alloc] initWithString:chaineMutable];

return returnedString;
}

然后你可以调用它:

NSString *difference = [self replaceInString:pippone :pippo :@""];

因此,您将在“pippone”中找到的“pippo”替换为“”(无),结果将是“ne”。

希望这可以帮助。

于 2013-05-12T11:01:49.673 回答
-3

更新答案:

-(void)getDifferenceBetweenString:(NSString *)firstString secondString:(NSString *)secondString{
    NSString *longerString = @"";
    NSString *shorterString = @"";
    if (firstString.length >= secondString.length) {
        longerString = firstString;
        shorterString = secondString;
    }else{
        longerString = secondString;
        shorterString = firstString;

    }
    NSArray *shorterArray = [self convertToArray:shorterString];
    NSArray *longerArray = [self convertToArray:longerString];
    NSMutableArray *differenceLettersArray = [NSMutableArray new];
    for (NSString * letter in shorterArray) {
        if (![longerString containsString:letter]) {
            if (![differenceLettersArray containsObject:letter]) {
                [differenceLettersArray addObject:letter];

            }
        }
    }


    for (NSString * letter in longerArray) {
        if (![shorterString containsString:letter]) {
            if (![differenceLettersArray containsObject:letter]) {
                [differenceLettersArray addObject:letter];

            }
        }
    }
    NSLog(@"differences = %@",differenceLettersArray);
}

- (NSArray *)convertToArray:(NSString *)string {
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    NSUInteger i = 0;
    while (i < string.length) {
        NSRange range = [string rangeOfComposedCharacterSequenceAtIndex:i];
        NSString *chStr = [string substringWithRange:range];
        [arr addObject:chStr];
        i += range.length;
    }

    return arr;
}

并按如下方式使用它:

[self getDifferenceBetweenString:@"pippo" secondString:@"pippone"];
于 2013-05-12T13:29:41.517 回答