获取所有表情符号在一个字符串中的位置,并将图像正确放置在某些位置 [英] Get the positions of all smileys in a string and place image properly at places

查看:213
本文介绍了获取所有表情符号在一个字符串中的位置,并将图像正确放置在某些位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用C#

开发聊天应用程序,我有一个这样的字符串

Hi I am developing a chat application in C#
and, I have a string like this

"Hi this is a test chat. :D Hope you are all doing fine:) <3 Please ping me back when you get online ;)" 





如何在这个字符串中获得所有表情符号的位置,而且

我必须将笑脸图片插入正确的位置。即,获取表情符号之间的子字符串并在写完每个子字符串后粘贴图片...我正在使用RIchText Box进行显示...

请帮助!!



How can I get positions of all smileys in this string, and moreover
I have to insert the smiley picture at the correct position. ie, get the sub strings between the smileys and paste the picture after writing each sub string... I am using RIchText Box for displaying...
Please Help!!

推荐答案

首先,Linq可能不是最好的解决方案。

正则表达式可以为你做到这一点:

First things first, Linq is probably not the best solution.
A regex can do it for you though:
string s = "Hi this is a test chat. :D Hope you are all doing fine:) <3 Please ping me back when you get online ;)";
Regex reg = new Regex(@":D|:\)|<3|;\)");
MatchCollection matches = reg.Matches(s);
foreach (Match match in matches)
    {
    Console.WriteLine("{0}:{1}", match.Value, match.Index);
    }


上面的OriginalGriff答案是查找匹配及其索引的优雅解决方案,您可以使用该代码浏览插入RTF控件中的内容,并用适当的位图/ Gif Smiley图像替换匹配。



如果你想忙着更换RTF,没有预先计算匹配和索引的正式步骤,然后你可以使用另一种策略,例如这个WinForms代码:



1.绑定你所有的笑脸/将图像转换为.NET应用程序中的Resources。或者你可以把它们放在一个ImageList中。



2.创建一个Dictionary字典的字典< string,image>这将把笑脸的字符串表示保存为键,并将笑脸图像资源的链接保存为值。因此,在某些时候,你将逐步建立字典,Smiley by Smiley,或逐个项目,如下所示:
OriginalGriff's answer above is an elegant solution to finding matches, and their indexes, and you can use that code to then go through the content inserted in an RTF Control and replace the matches with the appropriate bitmap/Gif Smiley image.

If you want to just get busy replacing the RTF, without the formal step of pre-calculating matches, and indexes, then you could use another strategy, such as this WinForms code:

1. "bind" all your Smiley/images into Resources in your .NET app. Or you could put them in an ImageList.

2. create a Dictionary of the form Dictionary<string,image> that will hold the string representations of the Smileys as Keys, and a link to the Smiley image resources as Values. So, at some point, you'll build-out the Dictionary, Smiley by Smiley, in a loop, or item-by-item, like this:
// you will need to use a reference to your app's Properties !
using YourRichTextApp.Properties;

EmoticonDict.Add(":)", MyChatApp.Properties.Resources.Smiley_Smile);

然后你只需遍历字典中的所有键,并使用内部' while循环继续用图像替换每个Key中文本的每个实例。



通过在'while'开始之前将图像复制到剪贴板来进行替换。循环,并在每次需要时粘贴它。



所以,你可以使用这样的东西:

Then you simply iterate through all the Keys in your Dictionary, and use an internal 'while loop to keep replacing every instance of the text in each Key with the image.

Do the replacement by copying the image to the Clipboard before the start of the 'while loop, and pasting it in every time you need to.

So, you'll can use something like this:

rtfText = RTFTextBox.Text;

foreach(string theKey in EmoticonDict.Keys)
{
   // put the Emoticon image on the Clipboard
   Clipboard.SetImage(EmoticonDict[theKey]);

   while (rtfText.Contains(theKey))
   {
      emoticonIndex = rtfText.IndexOf(theKey);
      rtfText = rtfText.Remove(emoticonIndex, theKey.Length);
      RTFTextBox.Select(emoticonIndex, theKey.Length);
      RTFTextBox.Paste();
      rtfText = RTFTextBox.Text;
   }
   
   // clean-up
   Clipboard.Clear();
}





鸣谢:此代码已由我根据Krishan Galore在2012年发布的代码进行了调整[ ^ ]。



Krishan使用HashTable,我使用Dictionary创建字符串/图像KeyValuePairs。使用字典消除了使用HashTable需要转换为Type Image的需要。



每次执行'while循环时,我都消除了使用Emoticon图像加载剪贴板。



我已经修复了该代码的问题:它没有正确清除表情字符串。



请注意,您可以使用此代码将任意字符串替换为任意图片资源。



Acknowledgement: this code has been adapted by me from code originally posted by Krishan Galore in 2012 [^].

Krishan used a HashTable, and I use a Dictionary for creating string/image KeyValuePairs. Using a Dictionary eliminates the need to cast to Type Image that using a HashTable requires.

I eliminated loading the Clipboard with the Emoticon image each time the 'while loop is executed.

I have "fixed" a problem with that code: it does not properly clear the emoticon-string.

Note that you could use this code to replace any arbitrary string with any arbitrary picture resource.


这篇关于获取所有表情符号在一个字符串中的位置,并将图像正确放置在某些位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆