Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am developing a chat application in C#
and, I have a string like this
C#
"Hi this is a test chat. :D Hope you are all doing fine:) <3 Please ping me back when you get online ;)" 


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!!
Posted

First things first, Linq is probably not the best solution.
A regex can do it for you though:
C#
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);
    }
 
Share this answer
 
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);
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:
C#
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();
}


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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900