0

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.

Siegfried.V
  • 1,508
  • 1
  • 16
  • 34
  • 3
    How about switching the order of the calls to .Replace? – Lasse V. Karlsen Jun 04 '21 at 06:39
  • 2
    Why not use Regex and do only a string format after that? – Stefano Cavion Jun 04 '21 at 06:42
  • @LasseV.Karlsen no I can't, this is a small example, but in concrete I don't know how many elements I will need to replace, nore in which order they will come. I am looking for a solution that would work whatever the order/quantity of replaces (the only solution I see would be to first replace elements by "----", "---", "--","-" (temporary strings), then replace them again by new elements, but it doesn't look like a nice solution – Siegfried.V Jun 04 '21 at 06:42
  • @StefanoCavion like your idea, but have no idea how to apply that in practical (very few experience on redex, only used them to filter strings). – Siegfried.V Jun 04 '21 at 06:44
  • Store the string in an array, the array should contain a pair of string+bool values, where bool means "ok to replace". Then, run your first replacement on all the strings (which is just 1) that has "ok to replace". Use regex to replace, which will give you positions. Split these strings into more strings back into the array and mark those that are replaced as "NOT ok to replace", then keep doing this for all replacements. – Lasse V. Karlsen Jun 04 '21 at 06:50
  • 3
    See [this answer](https://stackoverflow.com/a/12007487/3538012) in the duplicate – Peter Duniho Jun 04 '21 at 06:56
  • Why is it marked as duplicate? It is not at all the same question here... – Siegfried.V Jun 04 '21 at 06:56
  • _"It is not at all the same question here"_ -- it is exactly the same question. Part of being a programmer is knowing how to generalize. Just because the duplicate question doesn't involve the exact same text you're dealing with, that doesn't keep it from being a duplicate. The _technique_ is identical to what you need here. – Peter Duniho Jun 04 '21 at 06:57
  • @PeterDuniho thanks, if you think redex can solve this question, will try right now – Siegfried.V Jun 04 '21 at 06:58
  • @PeterDuniho yes in fact, saw your answer just after writting, I saw that question already, but accepted answer is not the good one I think, maybe redex will solve that. – Siegfried.V Jun 04 '21 at 06:59
  • No, you're right...often you have to look past the _accepted_ answer, to other answers that use different techniques. There are a number of similar questions already on the site, with a wide variety of answers (but often just doing the same chained "replace" you are trying to avoid). But the regex approach processes the string in a single operation. The alternative is to do that manually, by searching for _every_ possible replacement target in the input string, and replacing them explicitly once you've identified all the replacements. But regex will do that for you. – Peter Duniho Jun 04 '21 at 07:02
  • By the way, it's reGex, not reDex. Short for "REGular EXpressions". – Peter Duniho Jun 04 '21 at 07:03
  • The question is on the surface not a good duplicate, but it has an answer that answers it perfectly. – Lasse V. Karlsen Jun 04 '21 at 07:07
  • @PeterDuniho well seems reGex is working fine. Then just one question(or comment I don't know), isn't it possible in the title "This is a duplicate"(all top of the page), to edit the link to the correct answer as you did in your comment? Cause I don't know I did it correctly or not, I read lot of similar questions, then all accepted answers I saw didn't answer to "chain replace" issue. So I think this redex answer is much better than any other. – Siegfried.V Jun 04 '21 at 07:19
  • _"isn't it possible in the title "This is a duplicate"(all top of the page), to edit the link to the correct answer as you did in your comment?"_ -- unfortunately, no. The target of the closure has to be the _question_, and it doesn't have a way to specify an exact answer. Stack Overflow is very useful in a lot of ways, but its navigation features do leave something to be desired. Unless they eventually improve that, you'll just have to get into the habit of scrolling down to read all the answers, to find the one you actually need. :( – Peter Duniho Jun 04 '21 at 07:23
  • For what it's worth, most of us who will close a question as a duplicate _will_ also include a comment that indicates which answer specifically is the one you should look at, in those cases where there are multiple answers and the accepted answer is actually not useful. It comes up less often than you'd think though. – Peter Duniho Jun 04 '21 at 07:24
  • @PeterDuniho in fact it's the first time I got in that situation(I just "skipped" the regex answer as I didn't know how to use them), will edit my question refering to it, so the next who reads it can find quickly. Thanks anyway – Siegfried.V Jun 04 '21 at 07:29

0 Answers0