I have a string like this:
string source = hello{1}from{2}my{3}world
And an array like this:
var valArray = new[] { "Val0", "Val1", "Val2", "Val3" };
I want to replace "{index}" substring with appropriate values from array.
I've already written the code, but it looks ugly
var valArray = new[] { "Val0", "Val1", "Val2", "Val3" };
var source = "hello{1}from{2}my{3}world";
var substr = source;
string pattern = $"{Regex.Escape(start)}{".+?"}{Regex.Escape(end)}";
foreach (Match m in Regex.Matches(source, pattern))
{
var value = m.Groups[0].Value;
var ind = Convert.ToInt32(m.Groups[0].Value.TrimStart(start.ToCharArray()).TrimEnd(end.ToCharArray()));
substr = substr.Replace(value, valArray[ind]);
}
return substr;
Any tips how to get this solved?
Thanks!