A Function To Find Strings Between A Given Word/String

Updated sandeepparekh9 0 Tallied Votes 442 Views Share

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;
      }
samueal 0 Junior Poster

My opinion is
Using Regex will make it simply..

sandeepparekh9 109 Posting Whiz

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.