darma teja

darma teja

  • NA
  • 496
  • 338.5k

get text between two words in a string

Jan 24 2017 7:12 AM
i have string like this:
string myOrder= " \"orderID\":\"123456\",\"orderName\":\"ABC\",\"orderDate\":\"20170120\""
I am trying to get values of orderID, orderName, orderDate by the following method:
List<string> orderName= ExtractFromString(myOrder, "\"orderName\":\"", "\",");
private static List<string> ExtractFromString(string text, string startString, string endString)
{
List<string> matched = new List<string>();
int indexStart = 0, indexEnd = 0;
bool exit = false;
while (!exit)
{
indexStart = text.IndexOf(startString);
indexEnd = text.IndexOf(endString);
if (indexStart != -1 && indexEnd != -1)
{
matched.Add(text.Substring(indexStart + startString.Length,
indexEnd - indexStart - startString.Length));
text = text.Substring(indexEnd + endString.Length);
}
else
exit = true;
}
return matched;
}
I am getting null exception at matched.add(...
Advance thanks,
Darma

Answers (3)