As mentioned Peter duniho, here is the link to correct answer (not the accepted one), that uses regex.
I need to replace several elements of my string, One concrete example :
string originalString="#8= IFCPERSONANDORGANIZATION(#2,#4,$);";
string result=originalString.Replace("#8=","#2=");
result=result.Replace("#2,","#4,");
result=result.Replace("#4,","#1,");
But here, result will be :
#2= IFCPERSONANDORGANIZATION(#1,#1,$);
What I want is that :
#2= IFCPERSONANDORGANIZATION(#4,#1,$);
How can I make a "multi replace", avoiding that an element would be replaced twice?
Edit :
So here is a bigger part of the code :
Dictionary<int, int> replacementItems = new Dictionary<int,int>{{1,2},{2,4},{4,1}/*etc*/}
string stringOri="#8= IFCPERSONANDORGANIZATION(#2,#4,#1,#10$);";
foreach (int to_replace in replacementItems.Keys)
{
stringOri = stringOri.Replace("#" + to_replace + ")", "#" + replacementItems[to_replace] + ")")
.Replace("#" + to_replace + "=", "#" + replacementItems[to_replace] + "=")
.Replace("#" + to_replace + ",", "#" + replacementItems[to_replace] + ",");
}
The first example was just to explain problem more simply. In realitu, I never know how many items I need to replace, so change order of Replaces will not help.