Dima  Trygodko Dima Trygodko - 10 days ago 6
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

Hope this helps you

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

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.

Comments