# C# Replace in order?

There is a line:
``````string str = {1/2/3}{1/2/3}{1/2/3}{1/2/3}
string[] char = new string{"3","2","1","2"};``````

and I need to replace 1st {1/2/3}(str) to 3 1st(char), 2, {1/2/3}(str) to 2inch 2(char), 3rd {1/2/3}(str) to 3 1(char), etc. that would get str = 3212.

P. S. thank you All!
June 10th 19 at 14:23
June 10th 19 at 14:25
Solution
I give! And generally teach math part, due to the freebies in the toaster will not last long.
``````static void Main(string[] args)
{
var str = "{1/2/4}{1/3/3}{2/2/3}{1/1/3}";
var number = new [] { "3", "2", "1", "2" };

var temp = str
.Trim('{', '}')
.Split(new []{ "}{" }, StringSplitOptions.None)
.Select(x => x.Split('|').ToArray())
.ToArray();

var result = new StringBuilder();
for (var i = 0; i < temp.Length; i++)
{
var isParse = int.TryParse(number[i], out int index);
if (isParse)
{
result.Append(temp[i][index - 1]);
}
}

Console.WriteLine(result);
}``````
It's not quite what the text might be:
string str = "{1/2/3} text {1/2/3} tut {1/2/3} no and {1/2/3} where"
I was using Replace():
``````List<string> allKey = new List<string>();
List<string> key = new List<string>(); // this is the type of array number where {3,2,1,2}

foreach (Match match in Regex.Matches(str, "\\{([^\\}]+)\\}"))
{
}
foreach (string sKey in allKey)
{
string[] rndKey = sKey.Split('|');
int iKey = rnd.Next(rndKey.Length); // Random
}
for (int g = 0; g < allKeySubject.Count; g++)
{
subject = subject.Replace("{" + allKeySubject[g] + "}", keySubject[g]); // Here is the problem, if we use str data in {} are different then everything is OK, but if it braces {} are identical, they will be replaced by all 1 value =(
}</string></string></string></string>``````
- kirk22 commented on June 10th 19 at 14:28
as asked and answered. Your option won't work, at least from the wrong of the regular season. Or are You again not telling me something?) Second replays can only be guaranteed if the templates are unique, otherwise will be replaced everywhere.

Now another option distribute. But it will be quick. - Abner32 commented on June 10th 19 at 14:31
thank you very much) - kirk22 commented on June 10th 19 at 14:34
here so like works
``````static void Main(string[] args)
{
var subject = "{1/2/4} text, {1/3/3} tut {2/2/3} no and {1/1/3}";
var keys = new [] { "3", "2", "1", "2" };

var parts = subject.Split('{', '}');

var result = new StringBuilder();

for (var i = 0; i < parts.Length; i++)
{
if (i % 2 != 0)
{
var templates = parts[i].Split('|');
var isParse = int.TryParse(keys[i/2], out var key);
if (isParse)
{
result.Append(templates[key - 1]);
}
}
else
{
result.Append(parts[i]);
}
}

Console.WriteLine(result);
}``````
- Abner32 commented on June 10th 19 at 14:37
Thank you very much!!! Respect to YOU!) and this question that you had in vido under: "And generally teach math part"? Thanks again!!! - kirk22 commented on June 10th 19 at 14:40
, syllable by syllable, and the text of the question, I have a feeling that the question asks a man who could not solve the homework in the first year, because even the language syntax does not know. I usually ignore such issue, I do not like freeloaders) But then the code was 15 minutes, and I decided to answer. But in the real world in the total amount of response the hours spent)) - Abner32 commented on June 10th 19 at 14:43
Thank you again big!) - kirk22 commented on June 10th 19 at 14:46
Yes please) I did so quickly, then the parity zamorochili and made a version completely in LINQ
``````static void Main(string[] args)
{
var subject = "one {1/2/4} text, {1/3/3} tut {2/2/3} no and {1/1/3}";
var keys = new [] { "3", "2", "1", "2" };

var intKeys = keys
.Select(x => int.TryParse(x, out var key) ? key : -1 )
.ToArray();

var result = subject
.Split('{', '}')
.Select((x, i) => (i % 2 != 0 && intKeys[i/2] >= 0) ? x.Split('|').ElementAt(intKeys[i/2] - 1) : x)
.Aggregate((x, y) => x + y);

Console.WriteLine(result);