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 9147695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:03:37+00:00 2026-06-17T11:03:37+00:00

I’m trying to check a string for key words, and if the word exists,

  • 0

I’m trying to check a string for key words, and if the word exists, get the value from the dictionary. The problem exists when the key word is a multi-word phrase.

So I have a dictionary:

Dictionary<string, string> d = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

d.Add("keyword1", "D2");
d.Add("keyword2", "D3");
d.Add("keyword3", "D4");
d.Add("keyword4", "D4");
d.Add("keyword5", "D5");
d.Add("key word six", "D6"); 

And I have a string, which may look like the following but will be a random sentence:

string errormessage = "This is an error regarding Key Word Six";

I’m currently using the following to check the errormessage and see if any words appear in the dictionary:

string code = null;
string theDcode = null;

foreach (string word in errormessage.Split(' '))
{
    if (d.TryGetValue(word, out theDcode))
    {
        code = theDcode;
    }
}

The problem is that I can’t search for the string “Key Word Six” since I’m reading the string word by word and the foreach loop sees each word separately. This works great for single word key words. How can I handle checking for a multi word key word?

  • 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-06-17T11:03:39+00:00Added an answer on June 17, 2026 at 11:03 am

    You could iterate the items in the dictionary instead and check the string for matches.

    string errormessage = "This is an error regarding Key Word Six";
    var d = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
    
    d.Add("keyword1", "D2");
    d.Add("keyword2", "D3");
    d.Add("keyword3", "D4");
    d.Add("keyword4", "D4");
    d.Add("keyword5", "D5");
    d.Add("key word six", "D6"); 
    
    string code = null;
    foreach (var item in d)
    {
        var i = errormessage.IndexOf(item.Key, StringComparison.CurrentCultureIgnoreCase);
        if(i >= 0)
            code = item.Value;
    }
    

    This would however also give you matches inside of words. testkeyword1test for example.

    EDIT
    For possible better performance (untested) you could use a regular expression.

    string code;
    var reg = new Regex(GetPatternString(d.Select (x => x.Key)), RegexOptions.IgnoreCase);
    foreach (var match in reg.Matches(errormessage))
    {
        code = d[match.ToString()];
    }
    

    And helper function

    private static string GetPatternString(IEnumerable<string> values)
    {
        var sb = new StringBuilder();
        foreach (var oldStr in values)
        {
            sb.Append("(");
            sb.Append(Regex.Escape(oldStr));
            sb.Append(")|");
        }
        return sb.ToString(0, sb.Length - 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I am trying to understand how to use SyndicationItem to display feed which is

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.