user3085540
user3085540

Reputation: 295

Replace string based on array element

I have a array with some value and also having some parameter string.I have placed both below. I would like to replace the parameter string based on array element. Please help me to do thi.

string[] arrayval = {"Type:1","Action:doit","Message:hai"};

string Param = "Type:[Type]#Action:[Action]#Message:[Message]#OutMsg:[OutMsg]#@RetVal:[RetVal]";

My Expected Output 


string Param = "Type:1#Action:doit#Message:hai#OutMsg:[OutMsg]#@RetVal:[RetVal]";

Upvotes: 1

Views: 178

Answers (4)

cnd
cnd

Reputation: 33764

not tested and some checks should be added but, see in version of @Ulugbek Umirov :)

var pattern = @"\[(.*?)\]";
var matches = Regex.Matches(Param, pattern);

foreach (Match m in matches) {
    var inb = "[" + m.Groups[1] + "]";
    var results = arrayval.Select (s  => s.Split(':'))
                          .FindAll(ss => ss[0] == m.Groups[1])
                          .Select (ss => ss[1]);
    Param = Param.Replace(inb, results[0]);
}

btw... a bit shorter version of @Ulugbek Umirov which should work just fine :)

Param = Regex.Replace(Param, @"\[(.+?)\]", m =>
    arrayval.Select(s => s.Split(new[] { ':' }, 2))
            .Where (p => p.Length == 2)
            .Where (p => p[0] == m.Groups[1].Value)
            .Select(p => p[1])
            .FirstOrDefault() ?? m.Value);

Update for replace with 0 if not exists:

Param = Regex.Replace(Param, @"\[(.+?)\]", m =>
    arrayval.Select(s => s.Split(new[] { ':' }, 2))
            .Where (p => p.Length == 2)
            .Where (p => p[0] == m.Groups[1].Value)
            .Select(p => p[1])
            .FirstOrDefault() ?? "0");

Upvotes: 3

Ulugbek Umirov
Ulugbek Umirov

Reputation: 12807

Slightly modified version of @Heather

Param = Regex.Replace(Param, @"\[(.+?)\]", m =>
{
    string paramName = m.Groups[1].Value;
    string paramValue = arrayval.Select(s => s.Split(new[] { ':' }, 2))
                                .Where(p => p.Length == 2)
                                .Where(p => p[0] == paramName)
                                .Select(p => p[1])
                                .FirstOrDefault();
    return paramValue ?? m.Value;
});

Upvotes: 1

Neel
Neel

Reputation: 11741

Try below code..i have tested it in VS 2012

string[] arrayval = { "Type:1", "Action:doit", "Message:hai" };

string Param = "Type: " + arrayval[0].Split(Convert.ToChar(":"))[1] + "#Action: " + arrayval[1].Split(Convert.ToChar(":"))[1] + "#Message: " + arrayval[2].Split(Convert.ToChar(":"))[1];

Output :-

Type: 1#Action: doit#Message: hai

Upvotes: 0

Ehsan
Ehsan

Reputation: 32681

not very neat. But based on what information you have given. Following should work.

string[] arrayval = { "Type:1", "Action:doit", "Message:hai" };
string Param = string.Format("Type:{0}#Action:{1}#Message:{2}", arrayval[0].Split(':')[1], arrayval[1].Split(':')[1], arrayval[2].Split(':')[1]);

OutPut: Type:1#Action:doit#Message:hai

Upvotes: 3

Related Questions