Your attempt is close. There are two (possibly three issues) I found.
- Your string has a comma after the number you're looking for. Since your code is searching for the index of "data", your end index will end up one character too far.
- The second paramter of String.Substring(int, int) is actually a length, not an end index.
- Strings are immutable in C#. Because of this, none of the member functions on a string actually modify its value. Instead, they return a new value. I don't know if your code example is complete, so you may be assigning the return value of
SubString
to something, but if you're not, the end result is that strValue
remains unchanged.
Overall, the result of your current call to string.Substring
is returning 100,"data":[tes
. (and as far as I can see, it's not storing the result).
Try the following code:
string justTheNumber = null;
// Make sure we get the correct ':'
int startIndex = strValue.IndexOf("\"number\":") + 9;
// Search for the ',' that comes after "number":
int endIndex = strValue.IndexOf(',', startIndex);
int length = endIndex - startIndex;
// Note, we could potentially get an ArguementOutOfRangeException here.
// You'll want to handle cases where startPosition < 0 or length < 0.
string justTheNumber = strValue.Substring(startIndex, length);
Note: This solution does not handle if "number":
is the last entry in the list inside your string, but it should handle every other placement in it.
If your strings get more complex, you could try using Regular Expressions to perform your searches.