How do you detect words that start with “@” or “#” within an NSString?

前端 未结 6 1665
时光取名叫无心
时光取名叫无心 2020-12-09 21:49

I\'m building a Twitter iPhone app, and it needs to detect when you enter a hashtag or @-mention within a string in a UITextView.

How do I find all words preceded by

相关标签:
6条回答
  • 2020-12-09 22:05

    You can use NSRegularExpression class with a pattern like #\w+ (\w stands for word characters).

    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];
    NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
    for (NSTextCheckingResult *match in matches) {
        NSRange wordRange = [match rangeAtIndex:1];
        NSString* word = [string substringWithRange:wordRange];
        NSLog(@"Found tag %@", word);
    }
    
    0 讨论(0)
  • 2020-12-09 22:07
    if([[test substringToIndex:1] isEqualToString:@"@"] ||
       [[test substringToIndex:1] isEqualToString:@"#"])
    {
        bla blah blah
    }
    
    0 讨论(0)
  • 2020-12-09 22:12

    Use following expression to detect @ or # in string

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(#(\\w+)|@(\\w+)) " options:NSRegularExpressionCaseInsensitive error:&error];
    
    0 讨论(0)
  • 2020-12-09 22:18

    Made a category of NSString for that. It's very simple: Find all words, return all words that start with # to get the hashtags.

    Relevant code segment below - rename those methods & the category too...

    @implementation NSString (PA)
    // all words in a string
    -(NSArray *)pa_words {
        return [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    }
    
    // only the hashtags
    -(NSArray *)pa_hashTags {
        NSArray *words = [self pa_words];
        NSMutableArray *result = [NSMutableArray array];
        for(NSString *word in words) {
            if ([word hasPrefix:@"#"])
                [result addObject:word];
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-12-09 22:21

    You can break a string into pieces (words) by using componentsSeparatedByString: and then check the first character of each one.

    Or, if you need to do it while the user is typing, you can provide a delegate for the text view and implement textView:shouldChangeTextInRange:replacementText: to see typed characters.

    0 讨论(0)
  • 2020-12-09 22:26

    Here's how you can do it using NSPredicate

    You can try something like this in the UITextView delegate:

    - (void)textViewDidChange:(UITextView *)textView
    {
        _words = [self.textView.text componentsSeparatedByString:@" "];
        NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] '@'"];
        NSArray* names = [_words filteredArrayUsingPredicate:predicate];
        if (_oldArray)
        {
            NSMutableSet* set1 = [NSMutableSet setWithArray:names];
            NSMutableSet* set2 = [NSMutableSet setWithArray:_oldArray];
            [set1 minusSet:set2];
            if (set1.count > 0)
                NSLog(@"Results %@", set1);
        }
        _oldArray = [[NSArray alloc] initWithArray:names];
    }
    

    where _words, _searchResults and _oldArray are NSArrays.

    0 讨论(0)
提交回复
热议问题