1

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]";
1
  • 1
    Why don't you use dictionary instead of an array? Commented Mar 28, 2014 at 4:40

4 Answers 4

3

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");
Sign up to request clarification or add additional context in comments.

10 Comments

@Neel as in question declared
why to use so many line of codes when you can do the samething in just 2 lines?
Please see my updated question. i need the same output while am giving the some strings in param.
@user3085540 isn't it already passed there with ?? m.Value ?
@Heather No, FirstOrDefault() would return null, and you try to get [1] from it.
|
3

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

Comments

1

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;
});

1 Comment

I need a small change in my question Action:*[Action] instead of old one. If action present in arrayval means i need the above output. Otherwise i need the below output "Type:1#Action:0#Message:hai#OutMsg:[OutMsg]#@RetVal:[RetVal]";
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.