用图像替换字符串字符

Replace string character with an image

我正在聊天应用程序中处理表情符号。当有人向我发送表情符号时,我收到了这种格式的消息:- 你好...(担心)你好吗(快乐)?

(worried) and (happy) are considerd as emoji

arremojivalue=[[NSArray alloc]initWithObjects:@"(worried)",@"(sad)",@"(bandit)",@"(wink)",@"(surprised)",@"(smirking)",@"(laugh)",@"(cool)",@"(stoned)",@"(smile)",@"(nerd)",@"(happy)",@"(evil-grin)",@"(tongue)",@"(lips-sealed)",@"(GIF)",@"(dull)", nil];

当我收到消息(字符串)时,我如何检查它是否包含来自 arremojivalue 的上述单词。我想将消息字符串中的 (worried) 和 (happy) 词替换为 'emoji'.

我试过这个:-

NSString *stremoji;
stremoji=[NSString stringWithFormat:@"%@",arremojivalue];

if ([message containsString:stremoji])
{
     message= [message stringByReplacingOccurrencesOfString:stremoji
                                                    withString:@"(emoji)"];

     cell.textLabel.text=message; //@"Emoji arrived";
}

首先请回答我如何从字符串中删除这些词(担心)(快乐)到'emoji'。

之后我想将那些 'emoji' 单词替换为 UIWebview 以显示 GIF 表情符号。

谢谢

这是使用正则表达式替换的理想情况。

NSArray *emojiList = @[@"(sad)",@"(happy)",@"(angry)"] // and so on ..

NSString *regex = [emojiList componentsJoinedByString:@" | "];
regex = [string stringByReplacingOccurencesOfString:@"(" withString:@"\("];

regex = [string stringByReplacingOccurencesOfString:@")" withString:@"\)"];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:nil];

NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"(emoji)"];

试试这个

NSArray *arremojivalue=[[NSArray alloc]initWithObjects:@"(worried)",@"(sad)",@"(bandit)",@"(wink)",@"(surprised)",@"(smirking)",@"(laugh)",@"(cool)",@"(stoned)",@"(smile)",@"(nerd)",@"(happy)",@"(evil-grin)",@"(tongue)",@"(lips-sealed)",@"(GIF)",@"(dull)", nil];

//
NSString *message = @"Hello...(worried) how are you(happy)?";

for (NSString *emoji in arremojivalue) {
    if ([message containsString:emoji]){
       message = [message stringByReplacingOccurrencesOfString:emoji withString:YourEmojiValue];//value for (worried) = @":)"
    }
}
 NSLog(@"message updated:%@",message);

//你问题的第二部分

NSDictionary *emojiDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                 @":)",@"(worried)",
                                 @"(:D)",@"(smile)",
                                 nil];
NSString *message = @"Hello...(worried) how are you(smile)?";

for (NSString *emojiKey in emojiDictionary.allKeys) {
    if ([message containsString:emojiKey]){
        message = [message stringByReplacingOccurrencesOfString:emojiKey withString:[emojiDictionary valueForKey:emojiKey]];
    }
}
 NSLog(@"message updated:%@",message);