Click here to Skip to main content
15,867,594 members
Articles / Web Development / HTML
Alternative
Tip/Trick

General string parsing techniques

Rate me:
Please Sign up or sign in to vote.
4.68/5 (6 votes)
28 Oct 2011CPOL 16.9K   2   10
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)


Written By
Architect
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... Pin
ChunkyStool1-Nov-11 12:14
ChunkyStool1-Nov-11 12:14 
GeneralRe: but I did approve it, I hope it does work :) Pin
rj451-Nov-11 9:40
rj451-Nov-11 9:40 
GeneralI submitted the regex alternative again. Maybe a few code c... Pin
ChunkyStool1-Nov-11 9:37
ChunkyStool1-Nov-11 9:37 
GeneralNot sure why my regex alternative wasn't good enough for pub... Pin
ChunkyStool1-Nov-11 8:34
ChunkyStool1-Nov-11 8:34 
GeneralRe: It would be pretty slow alternative in terms of performance. Pin
rj451-Nov-11 9:39
rj451-Nov-11 9:39 
GeneralHave you considered using regular expressions? Pin
ChunkyStool31-Oct-11 14:05
ChunkyStool31-Oct-11 14:05 
GeneralRe: Indeed, I have. Oddly enough, your alternate was removed. ... Pin
Andrew Rissing1-Nov-11 5:14
Andrew Rissing1-Nov-11 5:14 
Indeed, I have. Oddly enough, your alternate was removed. *shrug*
GeneralReason for my vote of 4 Definitely an improvement to tie int... Pin
Henry.Ayoola27-Oct-11 23:11
Henry.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
Andrew Rissing28-Oct-11 4:06 
GeneralReason for my vote of 4 pretty cool Pin
rj4527-Oct-11 17:12
rj4527-Oct-11 17:12 

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.