Dima  Trygodko Dima Trygodko - 1 year ago 79
C# Question

Remove a substring after a substring

I have a string, for example

"blabla{code}<br />blabla{code}<br />bla{code}<br />"

How to remove all entries of
<br />
after {code}?

I've tried this:

public string removeBR(string comment)
Regex codeRegex = new Regex("{code}<br />", RegexOptions.Singleline);
return codeRegex.Replace(comment, new MatchEvaluator(m =>
string value = m.Groups[0].Value;
return value.Remove(value.Length - 6);

It works but is there any easier way?

Answer Source

Hope this helps you

string input = "blabla{code}<br />blabla{code}<br />bla{code}<br />";
string output = input.Replace("{code}<br />", "{code}");

as String.Replace returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download