Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6231855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:00:38+00:00 2026-05-24T10:00:38+00:00

I have got a problem with my exercise. The input data is a set

  • 0

I have got a problem with my exercise. The input data is a set of sentences – string[] sentences – The exercise’s requirement is that how to find and replace emoticon (ex: :D) to according smiley image in each sentences, and then export them to .html file.

File text data define emoticon and smiley has a structure like that:

[imagename] tab [emoticon1] space [emoticon2] space [emoticon2]

smile.gif    :) :-) :=) (smile)
sadsmile.gif :( :-( :=( (sad)
laugh.gif    :D :-D (laugh)
...

The first issue is which C#’s data structure to store emoticon and smiley.

I'm happy :). How are you? -> I'm happy <img src="smile"> How are you?

The second issue is how I code to search and replace emoticon.

the last issue is, because the export file is html format, so we must encode html, may be we use HttpUtility.HtmlEncode(...) But the resultSentence contain <img ...> tag, so I think it invole to the sencond issue…

Please help me to solve those above problem. Thanks so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:00:39+00:00Added an answer on May 24, 2026 at 10:00 am

    First, you need to load the smiley “mappings” into a dictionary:

    Dictionary<string, string> LoadSmileys(string fileName)
    {
        var smileys = new Dictionary<string, string>();
        using (var reader = new StreamReader(fileName))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                string[] parts = line.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 1; i < parts.Length; i++)
                {
                    smileys[parts[i]] = parts[0];
                }
            }
        }
        return smileys;
    }
    

    Then, just loop over the keys, and replace each occurrence of the key with the corresponding image. To avoid the problem mentionned in your comment to Carra’s answer, just replace the longest keys first:

    StringBuilder tmp = new StringBuilder(originalText);
    foreach (var key in smileys.Keys.OrderByDescending(s => s.Length))
    {
        tmp.Replace(key, GetImageLink(smileys[key]));
    }
    

    Note the use of a StringBuilder, to avoid creating many instances of String.

    It’s obviously not the most efficient approach, but at least it’s simple… you can always try to optimize it later if it turns out to be a performance bottleneck.


    UPDATE

    OK, so there is still a problem if some of your smileys include reserved HTML characters like ‘<‘ or ‘>’… If you encode the text to HTML before replacing the smileys, these characters will be replaced with &lt; or &gt;, so the smileys won’t be recognized. On the other hand, if you encode the text after replacing the smileys with <img> tags, the tags will be encoded as well.

    Here’s what you could do:

    • assign a unique identifier to each smiley, something unlikely to appear in the original text, like a GUID
    • replace each occurrence of each smiley by the corresponding identifier (again, starting with the longest smiley)
    • encode the resulting text to HTML
    • replace each occurrence of each smiley identifier by the appropriate <img> tag

      var mapping = LoadSmileys(@"D:\tmp\smileys.txt");
      var smileys = mapping.Keys.OrderByDescending(s => s.Length)
                           .ToArray();
      
      // Assign an ID like "{93e8b75a-6837-43f8-95ec-801ed59bc167}" to each smiley
      var ids = smileys.Select(key => Guid.NewGuid().ToString("B"))
                       .ToArray();
      
      string text = File.ReadAllText(@"D:\tmp\test_smileys.txt");
      
      // Replace each smiley with its id
      StringBuilder tmp = new StringBuilder(text);
      for (int i = 0; i < smileys.Length; i++)
      {
          tmp.Replace(smileys[i], ids[i]);
      }
      
      // Encode the text to HTML
      text = HttpUtility.HtmlEncode(tmp.ToString());
      
      // Replace each id with the appropriate <img> tag
      tmp = new StringBuilder(text);
      for (int i = 0; i < smileys.Length; i++)
      {
          string image = mapping[smileys[i]];
          tmp.Replace(ids[i], GetImageLink(image));
      }
      
      text = tmp.ToString();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a problem with multi line data (data that contains newline \n).
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
I've got a problem I can't find any solution for. I have a textfile
I have got a problem with my codeigniter library. To transform database data into
I have got a problem in my logcat screen and the problem is that,whenever
I have got a problem with submitting data to Post method. I have a
I've got a little problem with an exercise I have to finish. We have
WCF service on IIS. I have got a problem with reading connection string from
I have got a problem with calling a global function, which takes a pointer
I have got the following problem since the server has safe mode turned on,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.