Skip to content

Instantly share code, notes, and snippets.

@weeksdev
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save weeksdev/4678d00adaa4fe480034 to your computer and use it in GitHub Desktop.

Select an option

Save weeksdev/4678d00adaa4fe480034 to your computer and use it in GitHub Desktop.
Parsing
public class SubstringResponse {
public string Value { get; set; }
public int loc1 { get; set; }
public int loc2 { get; set; }
}
public static SubstringResponse Substring(this string source, string startPos, string endPos) {
return Substring(source, new List<string>() { startPos }, endPos);
}
public static SubstringResponse Substring(this string source, List<string> startPos, string endPos) {
var loc1 = source.IndexOf(startPos[0]);
var loc2 = -1;
if (loc1 >= 0) {
loc1 = loc1 + startPos[0].Length;
if (startPos.Count > 1) {
for (var i = 1; i < startPos.Count; i++) {
loc1 = source.IndexOf(startPos[i], loc1);
if (loc1 < 0) {
return new SubstringResponse() { Value = "", loc1 = loc1, loc2 = loc2 };
} else {
loc1 = loc1 + startPos[i].Length;
}
}
}
loc2 = source.IndexOf(endPos, loc1);
if (loc2 > loc1) {
return new SubstringResponse() { Value = source.Substring(loc1, loc2 - loc1), loc1 = loc1, loc2 = loc2 };
} else {
return new SubstringResponse() { Value = "", loc1 = loc1, loc2 = loc2 };
}
} else {
return new SubstringResponse() { Value = "", loc1 = loc1, loc2 = loc2 };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment