Click here to Skip to main content
13,193,441 members (47,145 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 27 Oct 2011

General string parsing techniques

, 28 Oct 2011
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.

You may also be interested in...

Comments and Discussions

GeneralRe: You are probably correct, but I doubt if the difference is s... Pin
ChunkyStool1-Nov-11 12:14
memberChunkyStool1-Nov-11 12:14 
GeneralRe: but I did approve it, I hope it does work :) Pin
rj451-Nov-11 9:40
memberrj451-Nov-11 9:40 
GeneralI submitted the regex alternative again. Maybe a few code c... Pin
ChunkyStool1-Nov-11 9:37
memberChunkyStool1-Nov-11 9:37 
GeneralNot sure why my regex alternative wasn't good enough for pub... Pin
ChunkyStool1-Nov-11 8:34
memberChunkyStool1-Nov-11 8:34 
GeneralRe: It would be pretty slow alternative in terms of performance. Pin
rj451-Nov-11 9:39
memberrj451-Nov-11 9:39 
GeneralHave you considered using regular expressions? Pin
ChunkyStool31-Oct-11 14:05
memberChunkyStool31-Oct-11 14:05 
GeneralRe: Indeed, I have. Oddly enough, your alternate was removed. ... Pin
Andrew Rissing1-Nov-11 5:14
memberAndrew Rissing1-Nov-11 5:14 
GeneralReason for my vote of 4 Definitely an improvement to tie int... Pin
Henry.Ayoola27-Oct-11 23:11
memberHenry.Ayoola27-Oct-11 23:11 
GeneralRe: Easy enough to do. It is a minor code smell, but smelly it ... Pin
Andrew Rissing28-Oct-11 4:06
memberAndrew Rissing28-Oct-11 4:06 
GeneralReason for my vote of 4 pretty cool Pin
rj4527-Oct-11 17:12
memberrj4527-Oct-11 17:12 
Reason for my vote of 4
pretty cool

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

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

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