-1

For Example i have string Like

"//RemoveFromhere
 <div>
 <p>my name is blawal i want to remove this div </p>
  </div>
//RemoveTohere"

I want to use //RemoveFromhere as starting point from where and //RemoveTohere as ending point in between all character i want to remove

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 4
    What do you have so far? Please update your question so that it shows your relevant code in a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). – wazz Sep 10 '21 at 11:24
  • 2
    You can use indexOf, RegEx.Split ... – Cetin Basoz Sep 10 '21 at 11:24
  • Maybe this does answer your question : https://stackoverflow.com/questions/51891661/removing-text-between-2-strings – XouDo Sep 10 '21 at 12:01
  • Does this answer your question? [Remove HTML tags from string including &nbsp in C#](https://stackoverflow.com/questions/19523913/remove-html-tags-from-string-including-nbsp-in-c-sharp) – Hoshani Sep 16 '21 at 11:40
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 16 '21 at 12:41

2 Answers2

2
  var sin = "BEFORE//RemoveFromhere"+
    "<div>"+
    "<p>my name is blawal i want to remove this div </p>"+
    "</div>"+
    "//RemoveTohereAFTER";
  const string fromId = "//RemoveFromhere";
  const string toId = "//RemoveTohere";
  var from = sin.IndexOf(fromId) + fromId.Length;
  var to = sin.IndexOf(toId);
  if (from > -1 && to > from)
    Console.WriteLine(sin.Remove(from , to - from));
//OR to exclude the from/to tags
  from = sin.IndexOf(fromId);
  to = sin.IndexOf(toId) + toId.Length;
  Console.WriteLine(sin.Remove(from , to - from));

This gives results BEFORE//RemoveFromhere//RemoveTohereAFTER and BEFOREAFTER

See also a more general (better) option using regular expressions from Cetin Basoz added after this answer was accepted.

AlanK
  • 1,827
  • 13
  • 16
0
void Main()
{
    string pattern = @"\n{0,1}//RemoveFromhere(.|\n)*?//RemoveTohere\n{0,1}";
    var result = Regex.Replace(sample, pattern, "");
    Console.WriteLine(result);
}

static string sample = @"Here
//RemoveFromhere
 <div>
 <p>my name is blawal i want to remove this div </p>
  </div>
//RemoveTohere
Keep this.
//RemoveFromhere
 <div>
 <p>my name is blawal i want to remove this div </p>
  </div>
//RemoveTohere
Keep this too.
";
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39