I created config file with some texts and this file i added to resource.resx. Now i created string for this text.

// String will be contains all texts from resource file
string TextFromResources = Resource.konfigurace;

TextFromResources - > View:

#################
# Settings
#################

RealmID = 1

#################

For next step i need use function "IndexOf".

int value = TextFromConfig.IndexOf("RealmID");
value = TextFromConfig.IndexOf('=');
value2 = TextFromConfig.IndexOf("/r/n");
RealmID = int.Parse(TextFromConfig.Substring(value, value2));

I need that realmID return me int value "1", but now its return me nothing. Please, can you help with my problem?

If index will be correctly so it will be return me value "1" nothing more.

有帮助吗?

解决方案

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top