Created
November 5, 2012 23:35
-
-
Save Layoric/4021129 to your computer and use it in GitHub Desktop.
Helpful C# method to get value between two string identifiers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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