A Function to find strings Between a Given Word/string

3:27 AM
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


For this use the following Function:


public static List FindStringBetween(string strData,string strFindWhat)
      {
          List lstFound = new List();
          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;
      }

Please ignore the last line.

It will give you the complete list of the strings found.


0 comments:

Post a Comment