General string parsing tools. These are handy because it is ergonomic to think " I want between this and that".
I use this to parse HTML quickly. Here is an example to find all image id elements from a page. If you look in my
article[
^], I have an XML parser based on this.
int outLastMatch =0;
for(;;)
{
string result = StringUtilities.GetBetween(html.ToString(), "<img id", "/>", outLastMatch, out outLastMatch);
if (result == String.Empty)
{
break;
}
removeList.Add(result);
}
public class StringUtilities
{
public static string GetBetween(string source, string start, string end, int startMatch, out int lastMatch)
{
int startIndex = source.IndexOf(start, startMatch);
if (startIndex == -1)
{
lastMatch = -1;
return String.Empty;
}
int endIndex = source.IndexOf(end, startIndex + start.Length + 1);
if (endIndex == -1)
{
lastMatch = -1;
return String.Empty;
}
lastMatch = endIndex;
endIndex -= startIndex;
endIndex += end.Length;
return source.Substring(startIndex, endIndex);
}
public static void GetBetweenExcludeTokens(string source, string start, string searchEnd, ref int begin, out int end)
{
end = -1;
begin = source.IndexOf(start, begin) + start.Length;
if (begin == -1)
{
return;
}
end = source.IndexOf(searchEnd, begin);
}
public static string GetBetweenExcludeTokens(string source, string start, string end, int startMatch, out int lastMatch)
{
lastMatch = -1;
int startIndex = source.IndexOf(start, startMatch);
if ( startIndex == -1 )
{
return String.Empty;
}
startIndex += start.Length;
lastMatch = source.IndexOf(end, startIndex);
if ( lastMatch == -1 )
{
return String.Empty;
}
return source.Substring(startIndex, lastMatch - startIndex);
}