Ah, the substring needs to start after value
. Here's a simplified version of your example that works for me:
var text = "RealmID = 1\r\n";
var value = text.IndexOf('=');
var value2 = text.IndexOf("\r\n", value);
var id = int.Parse(text.Substring(value + 1, value2 - value));
Also, I modified the IndexOf
call for getting value2
so that it searches for "\r\n"
after the location of '='
.
Another option is to use a regular expression to find the RealmID value.
var regex = new Regex(@"RealmID = (\d+)");
var match = regex.Match(text);
int realmID = int.Parse(match.Groups[1].Value);