Help with C# (ExtractAll?)

Discussion in 'Code Snippets and Tutorials' started by Belaarx, Dec 4, 2008.

  1. Belaarx

    Belaarx Level I

    Joined:
    Sep 23, 2008
    Messages:
    135
    Likes Received:
    16
    Location:
    San Diego
    Hey everyone, I am picking up C# and I was wondering if anyone had an "ExtractAll" function for C# that they could post here with example usage. Thanks
     
  2. Belaarx

    Belaarx Level I

    Joined:
    Sep 23, 2008
    Messages:
    135
    Likes Received:
    16
    Location:
    San Diego
    Close this thread.

    Code (Text):
    1.  
    2.         public string[] GrabAllBetween(string strSource, string strStart, string strEnd)
    3.         {
    4.             List<string> Matches = new List<string>();
    5.  
    6.             for (int pos = strSource.IndexOf(strStart, 0),
    7.                 end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1;
    8.                 pos >= 0 && end >= 0;
    9.                 pos = strSource.IndexOf(strStart, end),
    10.                 end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1)
    11.             {
    12.                 Matches.Add(strSource.Substring(pos + strStart.Length, end - (pos + strStart.Length)));
    13.             }
    14.  
    15.             return Matches.ToArray();
    16.         }
    17.  
    credits to cx323