Question

I need to remove all emoijs from a NSString. So far i am using this NSString extension ...

- (NSString*)noEmoticon {
    static NSMutableCharacterSet *emoij = NULL;

    if (emoij == NULL) {
        emoij = [[NSMutableCharacterSet alloc] init];
        // unicode range of old emoijs
        [emoij removeCharactersInRange:NSMakeRange(0xE000, 0xE537 - 0xE000)];
    }

    NSRange range = [self rangeOfCharacterFromSet:emoij];

    if (range.length == 0) {
        return self;
    }

    NSMutableString *cleanedString = [self mutableCopy];

    while (range.length > 0) {
        [cleanedString deleteCharactersInRange:range];
        range = [cleanedString rangeOfCharacterFromSet:emoij];
    }

    return cleanedString;
}

... but that does not work at all. The range.length is always 0. So the general question is : How can i remove a range of unicode characters from a NSString?

Thanks a lot.

Was it helpful?

Solution

It seems to me that in the above code the emoij variable is eventually an empty set. Didn't you mean to addCharactersInRange: rather than to removeCharactersInRange:?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top