Reputation: 1192
Hi all i'm reading a file into a string variable, however i'm wondering what the best way to return only a specific element in the string into a new variable.
I have the following string data file:
[TEST1]
mem.consumed.average = 965608688
cpu.usagemhz.average = 18653
Hosts = 3
Points = 1535
RAM = 1535.96
CPU = 191.79
Powered_on VMs = 70
Powered_on RAM = 1049.02
Powered_on CPU = 410.15
Powered_on Points = 1051
Provisioned VMs = 74
Provisioned RAM = 1057.02
Provisioned CPU = 416.59
Provisioned Points = 1059
[TEST2]
mem.consumed.average = 298762549
cpu.usagemhz.average = 7782
Hosts = 4
Points = 1535
RAM = 1535.96
CPU = 191.79
Powered_on VMs = 54
Powered_on RAM = 303.00
Powered_on CPU = 266.29
Powered_on Points = 303
Provisioned VMs = 60
Provisioned RAM = 330.00
Provisioned CPU = 295.28
Provisioned Points = 330
I only want to return the values for "Hosts", so in the case above only Hosts = 3 (from [TEST1]) and Hosts = 4 (from [TEST2]). Any ideas would be most appreciated :)
Upvotes: 0
Views: 109
Reputation: 3995
Use the regex class.
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx
Upvotes: 1
Reputation: 8285
If you want to just search it as a string, why not use .indexOf function.
So it would be
int a = data.indexOf('hosts');
int b = data.indexOf('points');
then
string hosts data = data.Substring(a,b);
you will have to alter the indexs to get the exact value out but it should be easy enough Something like
string hosts data = data.Substring(a + 7,b - 2);
Upvotes: 0
Reputation: 34707
I would just read line by line and look for line.StartsWith("Hosts")
- when that is true, pull out what you need.
Upvotes: 1