"We live in an exciting time. The Internet has become almost ubiquitous throughout much of the world, bringing with it freedom of information and an unprecedented power to all. My passion is seeing businesses leverage that power effectively and economically." - Dave Ranck

Archive for C#

May
19

Find a String Between 2 Strings

Posted by: Dave Ranck | Comments (0)

Here’s a way to return a substring of string that is between 2 strings in C#. There are other options for doing this, such as RegEx, but this is clean and simple.

// This search returns the substring between two strings, so 
// the first index is moved to the character just after the first string.
int first = str.IndexOf("$string1$") + "$string1$".Length;
int last = str.LastIndexOf("$string2$");
string str2 = str.Substring(first, last - first);
 
Technorati Tags: ,,,

Technorati Tags: C#, Code, Code Sample, Visual Studio

Sponsored By :

 

 

Material in this site unless otherwise noted is Copyright David Ranck 2009, 2010