65.9K
CodeProject is changing. Read more.
Home

General string parsing techniques

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.68/5 (6 votes)

Oct 27, 2011

CPOL
viewsIcon

18600

I would have actually opted to remove the start index being passed in. Then, you can spruce up the calls with extension methods (albeit not necessary).Usage:removeList.AddRange(html.ToString().GetBetween("");Code:public static class StringUtilities{ ///...

I would have actually opted to remove the start index being passed in. Then, you can spruce up the calls with extension methods (albeit not necessary). Usage:
removeList.AddRange(html.ToString().GetBetween("<img id=", " />");
Code:
public static class StringUtilities
{
  /// <summary>
  /// Gets a string that has the provided start and end.
  /// </summary>
  /// <param name="source">The source string to search.</param>
  /// <param name="start">The start of the string.</param>
  /// <param name="end">The end of the string.</param>
  /// <param name="includeTokens">Indicates whether to include the token strings or not in the returned value.</param>
  /// <returns>A string starting with start and ending with end.</returns>
  public static IEnumerable<string> GetBetween(this string source, string start, string end, bool includeTokens = false)
  {
     int currentIndex, startIndex, endIndex;

     currentIndex = 0;

     while (currentIndex < source.Length)
     {
       if (((startIndex = source.IndexOf(start, currentIndex)) != -1)
        && ((endIndex = source.IndexOf(end, startIndex + start.Length)) != -1))
       {
          currentIndex = endIndex + end.Length;

          if (includeTokens)
            yield return source.Substring(startIndex, endIndex - startIndex + end.Length);
          else
            yield return source.Substring(startIndex + start.Length, endIndex - startIndex - start.Length);
       }
       else
       {
         yield break;
       }
     }
  }
}