iOS---文本去除表情和标点符号

2021-12-23  本文已影响0人  BabyNeedCare
#define EMOJI_CODE_TO_SYMBOL(x) ((((0x808080F0 | (x & 0x3F000) >> 4) | (x & 0xFC0) << 10) | (x & 0x1C0000) << 18) | (x & 0x3F) << 24);
#pragma mark ---  系统表情替换
+ (NSString *)stringReplaceWithFace:(NSString *)str {
    
    if ([UtilityClass strIsEmpty:str]) {
        return @"";
    }
    
    NSString *mutaStr = str;

    for (int i=0x1F600; i<=0x1F64F; i++) {
        if (i < 0x1F641 || i > 0x1F644) {
            int sym = EMOJI_CODE_TO_SYMBOL(i);
            NSString *emoT = [[NSString alloc] initWithBytes:&sym length:sizeof(sym) encoding:NSUTF8StringEncoding];
            
            mutaStr = [mutaStr stringByReplacingOccurrencesOfString:emoT withString:@""];
        }
    }
    
    return mutaStr;
}
#pragma mark ---  去掉所有的特殊字符和标点符号
+(NSString *)deleteCharacters:(NSString *)targetString {
    
    if ([UtilityClass strIsEmpty:targetString]) {
        return @"";
    }
    
    NSError *error = nil;
    NSString *pattern = @"[^a-zA-Z0-9\u4e00-\u9fa5]";//正则取反
    NSRegularExpression *regularExpress = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];//这个正则可以去掉所有特殊字符和标点
    NSString *string = [regularExpress stringByReplacingMatchesInString:targetString options:0 range:NSMakeRange(0, [targetString length]) withTemplate:@""];
    
    return string;
}
上一篇下一篇

猜你喜欢

热点阅读