user9248102
user9248102

Reputation: 299

How to check if a string matches multiple strings and return value based on match

Apologizes if the title doesn't make much sense, English isn't my native language.

What I am trying to do: 1. I have a list of strings 2. I want to check each of those strings against another list of strings 3. Depending which string they contain, the output will be different

In code, it looks like this:

public static Hashtable Matches = new Hashtable
{
    {"first_match", "One"},
    {"second_match", "Two"},
    {"third_match", "Three"},
    {"fourth_match", "Four!"},
    {"fifth_match", "Five"}
};

Now, I have a list of strings like this:

001_first_match
010_second_match
011_third_match

And I want to check if each string in the list exists in the hashtable (or maybe other data type appropriate for this situation, suggestions appreciated) and based on that, to take the value for the key.

For example: 001_first_match is in the hashtable with first_match key. If found, then I want to take the One value of it and use it.

I can't use ContainsKey because the list of strings isn't 100% exact as the keys. The key is contained within the string, but there's extra data in the string.

I hope it's not too confusing what I want to do.

Upvotes: 3

Views: 878

Answers (4)

jdweng
jdweng

Reputation: 34429

Try following linq :

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;


namespace ConsoleApplication58
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string[] inputs = { "001_first_match", "010_second_match", "011_third_match" };

            foreach (string input in inputs)
            {
                var results = Matches.Keys.Cast<string>().Where(x => input.Contains(x)).FirstOrDefault();
                Console.WriteLine("Input '{0}' found in HashTable : {1}", input,  (results == null) ? "False" : "True, key = '" + results + "', Value = '" + Matches[results] + "'");
            }
            Console.ReadLine();


        }
        public static Hashtable Matches = new Hashtable
        {
            {"first_match", "One"},
            {"second_match", "Two"},
            {"third_match", "Three"},
            {"fourth_match", "Four!"},
            {"fifth_match", "Five"}
        };
    }

}

Upvotes: 2

Matthew Watson
Matthew Watson

Reputation: 109607

You can use Linq to do this by enumerating over the hashtable, casting each item to DictionaryEntry, and seeing if any element of the list of strings contains the key from the hashtable:

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Demo
{
    class Program
    {
        public static void Main(string[] args)
        {
            var Matches = new Hashtable
            {
                {"first_match", "One"},
                {"second_match", "Two"},
                {"third_match", "Three"},
                {"fourth_match", "Four!"},
                {"fifth_match", "Five"}
            };

            var Targets = new List<string>
            {
                "001_first_match",
                "010_second_match",
                "011_third_match"
            };

            var matches =
                Matches.Cast<DictionaryEntry>()
                .Where(x => Targets.Any(s => s.Contains((string)x.Key)))
                .Select(v => v.Value);

            Console.WriteLine(string.Join("\n", matches)); // Outputs "Three", "One" and "Two".
        }
    }
}

Upvotes: 1

Ali Imran
Ali Imran

Reputation: 686

I can do this by two dimensional array hope it can help you.

public string test() 
        {
            string result="";
            string[,] Hashtable = new string[2,2]
{
    {"first_match", "One"},
    {"second_match", "Two"},

};
            string match = "001_first_match";

            for (int i = 0; i < Hashtable.GetLength(0); i++)
            {
               string test1=  Hashtable[i, 0];
               if (match.Contains(test1)) { result = Hashtable[i, 1]; }

            }
            return result;
        }

Upvotes: 0

Stevo
Stevo

Reputation: 1434

using System;
using NUnit.Framework;
using System.Collections.Generic;

namespace StackOverflow
{
    public class StringMatch
    {
        public Dictionary<string, string> Matches = new Dictionary<string, string>
        {
            { "first_match", "One" },
            { "second_match", "Two" },
            { "third_match", "Three" },
            { "fourth_match", "Four!" },
            { "fifth_match", "Five" }
        };

        public List<string> Strings = new List<string>
        {
            "001_first_match",
            "010_second_match",
            "011_third_match"
        };

        [Test]
        public void FindMatches()
        {
            foreach (var item in Strings)
            {
                foreach (var match in Matches)
                {
                    if (item.Contains(match.Key))
                    {
                        Console.WriteLine(match.Value);
                        break;
                    }
                }
            }
        }
    }
}

Upvotes: 0

Related Questions