Skip to content

Useful code for extracting the contents of a tag in C#

July 1, 2013
As stated in the title, here is a very useful code method for extracting the inner contents of a custom formed tag in C#: - 

The string "s" is the string containing the whole tag, and the string "tag" is the tag name.

string ExtractString(string s, string tag) { // You should check for errors in real-world code, omitted for brevity var startTag = "<" + tag + ">"; int startIndex = s.IndexOf(startTag) + startTag.Length; int endIndex = s.IndexOf("</" + tag + ">", startIndex); return s.Substring(startIndex, endIndex - startIndex); }
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: