0

I want to replace words in a string that matches a keywords stored in an array with data from matching column in a table.

My model is People

public class People()
{
public int Id { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
}

The method i have tried is :

public void ProcessString(string message)
{
 using (DBEntities db = new DBEntities())
            {
                var people = db.People.ToList();

                foreach(var person in people)
                {
                    string[] keyword = {"#Title", "#Name", "#Surname"};

                        for (int i=0; i<keyword.Length; i++)
                    {
                        string updatedString  = Regex.Replace(body, keyword[i], matchingcolumndata);

                    }

                }
}

So instead of matchingcolumndata in the string updatedString = Regex.Replace(body, keyword[i], matchingcolumndata); line i want to put the data that is matching the column in my table People. Please help with the question if its not straight to the point.

  • So you want to replace all of your string "columns"? – Tim Schmelter Jul 14 '16 at 08:14
  • 1
    You need to replace the keywords by what exactly ? – Zein Makki Jul 14 '16 at 08:14
  • I want to replace the words in a string that matches the keywords stored in the array by column's data. For example if there's **#Title** word in a string then replace it with **people.Title** data from the People table – Zonke-Bonke S'cekeleshe Msibi Jul 14 '16 at 08:24
  • Check this one: [http://stackoverflow.com/questions/12226691/replace-multiple-words-in-a-string-from-a-list-of-words](http://stackoverflow.com/questions/12226691/replace-multiple-words-in-a-string-from-a-list-of-words) [http://stackoverflow.com/questions/20220913/fastest-way-to-replace-multiple-strings-in-a-huge-string](http://stackoverflow.com/questions/20220913/fastest-way-to-replace-multiple-strings-in-a-huge-string) – Kaushik Thanki Jul 14 '16 at 08:25

2 Answers2

0

From my understand it may be you are looking for

foreach(var person in people)
            {
                string[] keyword = {"#Title", "#Name", "#Surname"};
                body= body.Replace(keyword[0],person.Title);
                body= body.Replace(keyword[1],person.Name);
                body= body.Replace(keyword[2],person.Surname);
            }
King
  • 179
  • 5
0

It doesn't make any sense to declare your keywords array inside the loop. If you want to do this in a neat way (and subject to modifications easily), you can create a map between the keyword and the property of the Person. Like this:

using (DBEntities db = new DBEntities())
{
    var people = db.People.ToList();

    // maps each keyword to a property in the Person class
    var keywordPropertyMapping = new Dictionary<string, Func<Person, string>>()
    {
        { "#Title", p => p.Title },
        { "#Name", p => p.Name },
        { "#Surname", p => p.Surname }
    };

    foreach (var person in people)
    {
        foreach(var keywordFunc in keywordPropertyMapping)
        {
            body = body.Replace(keywordFunc.Key, keywordFunc.Value(people));
        }
    }
}

For any new keyword, you just add one simple line inside the dictioanry keywordPropertyMapping, and the magic works.

If you're not familiar with Func<T, TResult> which is one critical part of the Linq magic, then read the Docs:

Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter.

You can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have one parameter that is passed to it by value, and that it must return a value.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63