Skip to content

Instantly share code, notes, and snippets.

@Layoric
Created November 5, 2012 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Layoric/4021129 to your computer and use it in GitHub Desktop.
Save Layoric/4021129 to your computer and use it in GitHub Desktop.
Helpful C# method to get value between two string identifiers
/// <summary>
/// Generic function to pull out string values between two identifiers
/// </summary>
/// <param name="document"></param>
/// <param name="startVal"></param>
/// <param name="endVal"></param>
/// <returns></returns>
private static List<string> GetAllValuesBetweenKeys(string document, string startVal, string endVal)
{
string temp = document;
List<string> result = new List<string>();
int currentLocation = 0;
while (temp.IndexOf(startVal) != -1)
{
string temp2 = temp.Substring(currentLocation);
if (temp2.IndexOf(startVal) == -1)
{
break;
}
string afterVal = temp2.Substring(temp2.IndexOf(startVal) + startVal.Length);
if (afterVal.IndexOf(endVal) == -1)
{
break;
}
string val = afterVal.Substring(0, afterVal.IndexOf(endVal));
result.Add(val);
currentLocation = temp2.IndexOf(endVal) + endVal.Length;
temp = temp2;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment