-3

我有一个要求,比如在目标 c 的字符级别上使用 UNION 和 INTERSECTION。

“字符串A”,“字符串B”

上述字符串的(UNION)情况下的结果:“StringAB” 上述字符串的(交集)情况下的结果:“String”

任何帮助,将不胜感激。

4

1 回答 1

0

在stackoverflow成员的帮助下,我终于解决了这些问题

路口:

NSMutableSet *set1 = [NSMutableSet setWithArray: array1];
NSSet *set2 = [NSSet setWithArray: array2];
[set1 intersectSet: set2];
NSArray *resultArray = [set1 allObjects];

UNION:连接两个字符串并从该字符串中删除重复的字符,最后你会得到 UNION :)

SString *input = @"addbcddaa";
NSMutableSet *seenCharacters = [NSMutableSet set];
NSMutableString *result = [NSMutableString string];
[input enumerateSubstringsInRange:NSMakeRange(0, input.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    if (![seenCharacters containsObject:substring]) {
        [seenCharacters addObject:substring];
        [result appendString:substring];
    }
}];
NSLog(@"String with duplicate characters removed: %@", result);
NSLog(@"Sorted characters in input: %@", [seenCharacters.allObjects sortedArrayUsingSelector:@selector(compare:)]);

谢谢!

于 2012-09-11T11:12:23.417 回答