Question

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?

Was it helpful?

Solution 4

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

OTHER TIPS

The standard solution for string formatting in C# (and in .NET in general) is to use the String.Format method. So your could for example do:

string reult = string.Format("The Name of the Item with the Id {0} is {1}. It's Description is {2}. It has the Value {3} and the Status {4}.",
       id, name, description, value, status);

I suspect you want to replace placeholders such as {ID} and {Name} with the values from your list of objects. You could use a regex with a patterns such as

\{(?<key>[A-Za-z]+)\}

Which will find all instances of {something} and allow you to extract the something in order to get a value from your list.

Using the overload of Regex.Match which takes an input string and a MatchEvaluator delegate, would allow you to get the right value:

var myObject = new Dictionary<string,string>(){
    {"ID","1"},  
    {"Name","Bob Jones"},
    {"Description","A very nice man"},
    {"Value","1000"},
    {"Status","New"},
};
var regex = new Regex(@"\{(?<key>[A-Za-z]+)\}");
var input = "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}.";
string result = regex.Replace(input, m => myObject[m.Groups["key"].Value]);

Console.WriteLine(result);

Live example: http://rextester.com/FLGTQW95860

The dictionary in that example is just to demonstrate the usage of the regex - there is no reason that couldnt be a DataRow or a custom object, or any other container for the properties.

Of course this solution does not contain any error handling (such as a placeholder where the property does not exist), which I suspect you will want to include if you're allowing the user to specify the string(s).

Do you just mean:

var x = String.Format("The Name of the Item with the Id {0} is {1}. It's Description is {2}. It has the Value {3} and the Status {4}.",object.Id, object.Name, object.Description, object.Value, object.Status); 

Or did I miss the point?

You could define you own class and override the ToString:

public class Program
{
    static void Main(string[] args)
    {
        var aString = new MyString() {Description = "desc", Id = 1, Name = "Ja", Status = "st", Value = "Val"};

        var myStrings = new List<MyString>() {aString};


        foreach (MyString myString in myStrings)
        {
            //The Name of the Item with the Id 1 is Ja. It's Description is desc. It has the Value val and the Status st.
            var outputStringVal = myString.ToString();
            //
        }
    }
}

public class MyString 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Value { get; set; }
    public string Status { get; set; }

    public override string ToString()
    {
        return String.Format("The Name of the Item with the Id {0} is {1}. It's Description is {2}. It has the Value {3} and the Status {4}.", this.Id, Name, Description, Value, Status);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top