kkakroo
kkakroo

Reputation: 1113

replacing data in between specific characters

I have a string which has some keys between <<>>.

 string s = "<p>Hi <<USER>>,<br/>How are you doing<br/>Regards,<br/><<SENDER>></p>";

I want to first fetch the key names USER and SENDER in a list which i did by:

    var keys = new List<string>();
            foreach (Match match in Regex.Matches(s, @"<<(.*?)>>")) 
            {
                keys.Add(match.Groups[1].Value);

            }
List<string> values= new List<string>(){"John","Team"};

After we get the keys,i want to replace these keys by another list(named values here) which has the values for these keys and want the result as:

 string s = "<p>Hi John,<br/>How are you doing<br/>Regards,<br/>Team</p>";

The string s can be anything and the no of keys and their values could also vary but the keys will always be enclosed in between <<>>

Upvotes: 4

Views: 228

Answers (4)

chomba
chomba

Reputation: 1451

As suggested by @AlexBell, you could simply use the String.Replace() method. Further, it's more convenient to declare a collection of placeholder/value pairs, like so:

string s = "<p>Hi <<USER>>,<br/>How are you doing<br/>Regards,<br/><<SENDER>></p>";

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("<<USER>>", "Jhon");
dictionary.Add("<<SENDER>>", "Team");

StringBuilder text = new StringBuilder(s);
foreach (var entry in dictionary)
{
    text.Replace(entry.Key, entry.Value);
}

Console.WriteLine(text.ToString());

Upvotes: 2

Haseeb Asif
Haseeb Asif

Reputation: 1786

//First we have lists of values (users) and senders
List<string> values= new List<string>(){"John","Team"};
List<string> senders = new List<string>(){"John","Team"};
//Then we can join that list using string.join
var allUsers = string.Join(",", values);
var allSender = string.Join(",", senders);

//Next we will be replacing it in our string
var namedString = Regex.Replace(string, @"<<USER>>", allUsers);
var output = Regex.Replace(namedString , @"<<SENDER>>", allSender);

Upvotes: 0

Stephen Kennedy
Stephen Kennedy

Reputation: 21568

This function will perform the replacements that you ask for, using Regex.Replace:

public static string ParseTemplate(string template, string username, string senderName)
{
    template = Regex.Replace(template, @"<<USER>>", username);
    return Regex.Replace(template, @"<<SENDER>>", senderName);
}

Example:

string s = "<p>Hi <<USER>>,<br/>How are you doing<br/>Regards,<br/><<SENDER>></p>";
ParseTemplate(s, "John", "Team").Dump();

Output:

<p>Hi John,<br/>How are you doing<br/>Regards,<br/>Team</p>

You can call this in a loop over your dictionary or list of names.

Upvotes: 2

Alexander Bell
Alexander Bell

Reputation: 7918

Your business logic is a bit unclear, so based on just qualified guess, you can apply standard .NET/C# String.Replace Method (String, String) (re:https://msdn.microsoft.com/en-us/library/fk49wtc1%28v=vs.110%29.aspx), for example:

string s = "<p>Hi <<USER>>,<br/>How are you doing<br/>Regards,<br/><<SENDER>></p>".Replace("<USER>", "John").Replace("<SENDER>", "Team");

Hope this may help.

Upvotes: 0

Related Questions