PeteTheGreek
PeteTheGreek

Reputation: 729

Remove a section of text from string c#

I'm using tags in a richtexteditor to specify data fields.

E.g [Start] and [End]

How can I remove a section of text between [Start] and [End] from a string block including the tags?

Is there an easier way rather than using IndexOf and Substring etc?

Update: I'm attempting to use var output = Regex.Replace("[Start]SomeText[End]", @"(?<=[Start]).*(?=[End])", "")

But the pattern does not quite work. It needs to remove everthing between [Start] and [End]

Upvotes: 0

Views: 651

Answers (2)

gpmurthy
gpmurthy

Reputation: 2427

Consider using Regex to replace the text between [Start] and [End]. The following code snippet should help...

var output = Regex.Replace("[Start]SomeText[End]", @"(?<=\[Start\]).*(?=\[End\])", "");

Upvotes: 2

user1666620
user1666620

Reputation: 4808

You could use a Regular Expression, but I can't think of anything 'easier' than searching the file for it.

Upvotes: 0

Related Questions