美文网首页
iOS---文本去除表情和标点符号

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

作者: BabyNeedCare | 来源:发表于2021-12-23 10:10 被阅读0次
    #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;
    }
    

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/dulhqrtx.html