0

我正在尝试编写一个函数,它可以让我确定一个是否NSString*包含另一个的字符NSString*。例如,请参考以下场景:

NSString *s1 = @"going";
NSString *s2 = @"ievngcogdl";

因此,本质上,当这两个字符串之间发生比较时,它应该返回 true,因为第一个字符串s1与第二个字符串具有相同的字符s2。我可以使用一个NSCountedSet吗?我知道这个类有一个方法,containsObject:(id)尽管我认为这不能解决我的问题。还有其他方法可以完成此功能并为我提供所需的结果吗?

4

4 回答 4

2

我认为这种方法可能会相当慢,但我仍然喜欢它,这需要在每次比较[NSString rangeOfCharacterFromSet:]时创建一个对象:NSCharacterSet

- (BOOL)string:(NSString *)string containsAllCharactersInString:(NSString *)charString {
    NSUInteger stringLen = [string length];
    NSUInteger charStringLen = [charString length];
    for (NSUInteger i = 0; i < charStringLen; i++) {
        unichar c = [charString characterAtIndex:i];
        BOOL found = NO;
        for (NSUInteger j = 0; j < stringLen && !found; j++)
            found = [string characterAtIndex:j] == c;
        if (!found)
            return NO;
    }
    return YES;
}
于 2014-10-17T10:40:56.723 回答
2

这将起作用 -

-(BOOL) string:(NSString *)string1 containsInputString:(NSString *)string2 {

    // Build a set of characters in the string

    NSCountedSet *string1Set = [[NSCountedSet alloc]init];

    [string1 enumerateSubstringsInRange:NSMakeRange(0, string1.length)
                                options:NSStringEnumerationByComposedCharacterSequences
                             usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                 [string1Set addObject:substring];
                             }];


    // Now iterated over string 2, removing characters from the counted set as we go
    for (int i=0;i<string2.length;i++) {
        NSRange range = [string2 rangeOfComposedCharacterSequenceAtIndex:i];
        NSString *substring = [string2 substringWithRange:range];
        if ([string1Set countForObject:substring]> 0) {
            [string1Set removeObject:substring];
        }
        else {
            return NO;
        }
    }
    return YES;
}
于 2014-10-17T10:48:16.967 回答
-1

正则表达式是检查此类条件并检查此链接一次的最佳方法

下面我为您的解决方案添加代码,请检查一次

  NSString *s1 = @"going"
  NSString *s2 = @"ievngcogdl";

  if ([self string:s1 containsSameCharacterofString:s2]) {

            NSLog(@"YES");

  }


    - (BOOL)string:(NSString *)str containsSameCharacterofString:(NSString *)charString
    {

        if (charString.length >= str.length) {

            NSError *error = nil;
            NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^[%@]+$", charString] options:NSRegularExpressionCaseInsensitive error:&error];

            NSRange textRange = NSMakeRange(0, str.length);
            NSRange matchRange = [regex rangeOfFirstMatchInString:str options:NSMatchingReportProgress range:textRange];

            return (matchRange.location != NSNotFound);

        }
        else {

            return NO;
        }

    }
于 2014-10-17T11:53:20.390 回答
-3
BOOL containsString = [@"Hello" containsString:@"llo"];
if (containsString) {
    // Do Stuff
}
于 2014-10-17T10:34:46.777 回答