0

I am having my string as "Date :<#Tag(SystemTagDateTime)> Value1 : <#Tag(value1)>" I want to replace SystemTagDateTime , value1 by its value and want to show whole string in Message Box as "Date :2016-05-18 10:00:00 Value1 : 10"

My Initial string may have one or more than one <#Tag(Anything)>.I have tried but failed to get desired value For Example

line -> Date :<#Tag(SystemTagDateTime)> Value1 : <#Tag(value1)>

string[] values =Regex.Split(line, "<#Tag\\(|\\)>").Where(x => x != string.Empty).ToArray();
string text = ""; 

foreach (string val in values) { 

    if (!(String.IsNullOrEmpty (val.Trim ())))
    { 
        foreach(GlobalDataItem gdi in Globals.Tags.GlobalDataItems) 
        { 
            MessageBox.Show(val);
            if (gdi.Name == val) 
            { 
                text+= gdi.Value; 
            } 
        } 
    } 
    else 
    { 
        text += val ;
    } 
}

1 Answer 1

2

This is a simple way how to resolve your task:

//Input string and we would like to keep it value
const string str = "Date :<#Tag(SystemTagDateTime)> Value1 : <#Tag(value1)>";
string text = str;
foreach (GlobalDataItem gdi in Globals.Tags.GlobalDataItems)
{
   //Preparing tag name. for instance, <#Tag(SystemTagDateTime)>
   string tag = string.Format("<#Tag({0})>", gdi.Name);
   //Replace the tag everywhere with value from gdi.
   text = text.Replace(tag, gdi.Value); 
}

In 'text'you'll have your string.

Sign up to request clarification or add additional context in comments.

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.