Search code examples
c#stringparsingvariablesparameterized

Allow user defined Strings in c#


i'm asking about a pretty common feature I could not find any information about. I want to allow the user of my program to create custom strings with variables from the program.

Example:

I have a list:

ID   Name     Description       Value      Status
0    name 0   beschreibung 0    Wert 0     Status 0
1    name 1   beschreibung 1    Wert 1     Status 1
2    name 2   beschreibung 2    Wert 2     Status 2
3    name 3   beschreibung 3    Wert 3     Status 3
4    name 4   beschreibung 4    Wert 4     Status 4
5    name 5   beschreibung 5    Wert 5     Status 5
6    name 6   beschreibung 6    Wert 6     Status 6
7    name 7   beschreibung 7    Wert 7     Status 7
8    name 8   beschreibung 8    Wert 8     Status 8
9    name 9   beschreibung 9    Wert 9     Status 9

Now the user shall be able to define custom strings like:

The Name of the Item with the Id {ID} is {Name}. It's Description is {Description}. It has the Value {Value} and the Status {Status}.

I could write a custom parsing routine for those strings, but I hope to find a standard solution for that task. Any suggestions?


Solution

  • You may to allow the user use specified "variables" in string. For example:

    var userstring = "The Name of the Item with the Id {ID} is {Name}. It's Description is {Description}. It has the Value {Value} and the Status {Status}.";
    var result = userstring.Replace("{ID}", Id).Replace("{Name}", Name).Replace("{Description}", Description).Replace("{Value}", Value).Replace("{Status}", Status);