Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

General string parsing techniques

, 28 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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).
removeList.AddRange(html.ToString().GetBetween("<img id=", " />");
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);
            yield return source.Substring(startIndex + start.Length, endIndex - startIndex - start.Length);
         yield break;


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


About the Author

Andrew Rissing
Software Developer (Senior)
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.
So..If you're not moving forward, you're moving backwards.

Comments and Discussions

GeneralRe: You are probably correct, but I doubt if the difference is s... PinmemberChunkyStool1-Nov-11 13:14 
GeneralRe: but I did approve it, I hope it does work :) Pinmemberrj451-Nov-11 10:40 
GeneralI submitted the regex alternative again. Maybe a few code c... PinmemberChunkyStool1-Nov-11 10:37 
GeneralNot sure why my regex alternative wasn't good enough for pub... PinmemberChunkyStool1-Nov-11 9:34 
GeneralRe: It would be pretty slow alternative in terms of performance. Pinmemberrj451-Nov-11 10:39 
GeneralHave you considered using regular expressions? PinmemberChunkyStool31-Oct-11 15:05 
GeneralRe: Indeed, I have. Oddly enough, your alternate was removed. ... PinmemberAndrew Rissing1-Nov-11 6:14 
GeneralReason for my vote of 4 Definitely an improvement to tie int... PinmemberHenry.Ayoola28-Oct-11 0:11 
GeneralRe: Easy enough to do. It is a minor code smell, but smelly it ... PinmemberAndrew Rissing28-Oct-11 5:06 
GeneralReason for my vote of 4 pretty cool Pinmemberrj4527-Oct-11 18:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150301.1 | Last Updated 28 Oct 2011
Article Copyright 2011 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid