Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

General string parsing techniques

4.68/5 (6 votes)
28 Oct 2011CPOL 18.6K  
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:

XML
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;
       }
     }
  }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)