"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

May
19

Find a String Between 2 Strings

By Dave Ranck

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

  • Share/Bookmark

Related posts:

  1. Restore SQL Database
  2. Create Singleton in C#
  3. Title Case in C#
  4. Snippet Designed for VS 2008 and VS 2010
  5. Move User Profile to 2nd Drive in Win 7

Leave a Reply

Sponsored By :

 

 

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