-1

I need to replace character sequences in a string, but they are not guaranteed not to be overlapping. The rule in case of conflicts is that ( first, the longest character sequence wins, but this is irrelevant), but if they are the same length the ones closest to the end must take priority. I would like to use a StringBuilder for this, but I'm not sure how to implement this.

The code below illustrates this:

string source = "abaaaababa"; var sb = new StringBuilder(source); Console.WriteLine("What I want:" + ReplaceFromRight(source,"aba","dgd")); Console.WriteLine("What I have:" + sb.Replace("aba", "dgd").ToString()); string ReplaceFromRight(string text, string from, string to) { for (int i = text.Length-from.Length; i >=0 ; i--) { if(text.Substring(i, from.Length) == from) text = text.Substring(0,i)+to+text.Substring(i+from.Length); } return text; } 
What I want:dgdaaabdgd What I have:dgdaadgdba 

(ReplaceFromRight is quick and dirty, I know, but it illustrates my purpose)

What is the best way to do this?

Here's what I came up with:

string ReplaceFromRightWithSb(StringBuilder sb, string from, string to) { var text = sb.ToString(); var lastIndex=text.LastIndexOf(from); while (lastIndex > -1) { sb.Replace(from, to, lastIndex, from.Length); text = sb.ToString(); lastIndex = text.LastIndexOf(from); } return sb.ToString(); } 

(it's basically what Tim said below, so I will accept his answer)

5
  • Replace always works from the start of the String(buillder) towards the end. A simple (but not necessarly the most efficient) way for ReplaceFromRight would be reversing the original string, search term and replacement term before calling Replace and again reverse the result afterwards Commented Mar 27 at 9:47
  • Is it only in my browser? Cannot seem to fix code highlighting. (I edited to fix a typo) Commented Mar 27 at 9:50
  • 1
    @Fildor, no it's not your browser. For some reason code markup shows in preview but not in the question. Commented Mar 27 at 9:59
  • var result = Regex.Replace("abaaaababa", "aba", "dgd", RegexOptions.RightToLeft); Commented Mar 27 at 10:01
  • If you want to provide the answer for future readers, then post the answer as the answer, don't edit it into question. Commented Mar 27 at 10:28

1 Answer 1

0

If i understood it right, this should work: Demo

public static string ReplaceFromRight(string text, string from, string to) { var sb = new StringBuilder(text); for (int i = text.Length - from.Length; i >= 0; i--) { if (text.Substring(i, from.Length) == from) { sb.Remove(i, from.Length); sb.Insert(i, to); text = sb.ToString(); } } return sb.ToString(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

I came up with a solution of my own (see above,) it's almost identical, but I wonder if Replace is less efficient than Remove + Insert. Does anyone know?
@VladimirsKacs: You can measure it, for example with github.com/dotnet/BenchmarkDotNet :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.