I am trying to fetch a particular field's value from a string using the below code.
Example Code:
string contents = "2018-07-31 \"streetAddress1\":{\"1 Harbour Street\"},\"streetAddress2\":{\"2 Lords Road\"}";
string streetAddress1 = ValueFinder(contents, "\"streetAddress1\"", "\"streetAddress2\"");
...
public string ValueFinder(string contents, string startText, string endText)
{
int indexPos1 = contents.IndexOf(startText);
int indexPos2 = contents.IndexOf(endText);
if(indexPos1 != -1 && indexPos2 != -1)
{
string finalValue = contents.Substring(indexPos1 + (startText.Length + 3), indexPos2 - indexPos1 - (startText.Length + 3));
return finalValue.Split(':')[1];
}
return "";
}
In the above code, I am able to get whats between the two fields. However, is there a simpler way to get the values between flower braces {} if I give a field's name.
Example, if field's name is "streetAddress1" then it should output its values as "1 Harbour Street" instead of my current logic where I have specified two fields in order to fetch the first field's value.
Thank you.