954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

A Function To Find Strings Between A Given Word/String

0
By sandeepparekh9 on Jun 2nd, 2011 2:41 pm

A Function To Find Strings Between A Given Word/String

For example :
let's say you have following Text:

Title Author Product*
~~~~~ ~~~~~~ ~~~~~~~~
"Aurora's Eggs" Douglas Niles Story (Dragons 2)

The Dragons Douglas Niles Novel

The Kinslayer Wars Douglas Niles Novel

The Qualinesti Paul B. Thompson Novel
& Tonya C. Cook

Vinas Solamnus J. Robert King Novel

The Dargonesti Paul B. Thompson Novel
& Tonya C. Cook

"Storytellers" Nick O'Donohoe Story (Dragons 2)

"The Best" Margaret Weis Story (Dragons 1)

"Quarry" Adam Lesh Story (Dragons 2)

"Easy Pickings" Douglas Niles Story (Dragons 1)

The Legend of Huma Richard A. Knaak Novel

And I want to find Everthing that is in between " character.

The Answer should be like Following

Aurora's Eggs

Storytellers

The Best

Quarry

Easy Pickings


Check here for Better Understanding: [snipped]

public static List<string> FindStringBetween(string strData,string strFindWhat)
      {
          List<string> lstFound = new List<string>();
          int startIndex, EndIndex;
          startIndex = strData.IndexOf(strFindWhat);
 
          EndIndex = strData.IndexOf(strFindWhat, startIndex + strFindWhat.Length);
 
          if (EndIndex > 0)
          {
              lstFound.Add(strData.Substring(startIndex+strFindWhat.Length ,EndIndex - startIndex-strFindWhat.Length )); 
          }
 
          while (EndIndex > 0)
          {
              startIndex = strData.IndexOf(strFindWhat, EndIndex + 1);
              if (startIndex  == -1) {
                  return lstFound; }
                  EndIndex = strData.IndexOf(strFindWhat, startIndex + 1);
              if (EndIndex > 0)
              {
                  lstFound.Add(strData.Substring(startIndex + strFindWhat.Length, EndIndex - startIndex - strFindWhat.Length)); 
              }
          }
 
          return lstFound;
      }

My opinion is
Using Regex will make it simply..

samueal
Junior Poster
111 posts since Apr 2011
Reputation Points: 18
Solved Threads: 16
 

ya .. i know .. i made this before i learned regex.
just wanted to share...
:)

sandeepparekh9
Posting Whiz
367 posts since Dec 2010
Reputation Points: 123
Solved Threads: 71
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You