Home > Software design >  split string and extract values to store in float variable
split string and extract values to store in float variable

Time:09-08

I want to split below string and need to read only values and store in some float variable kindly help how i can do that

string s = "PH_Value:7.539999961853027,Nitrate:1.0099999904632568,DO:10.229999542236328,Conductivity:6777.990234375,TSS:43.65999984741211";

CodePudding user response:

Give this a go, next time try to attempt it yourself:

List<float> results = new List<float>();
Dictionary<string, float> dictResults = new Dictionary<string, float>();
string s = "PH_Value:7.539999961853027,Nitrate:1.0099999904632568,DO:10.229999542236328,Conductivity:6777.990234375,TSS:43.65999984741211";

string[] arr = s.Split(',');
foreach(string item in arr)
{
    string[] subArr = item.Split(':');
    results.Add(subArr[1]); //a list
    dictResults.Add(subArr[0],subArr[1]); // a dictionary for easy lookup
}

CodePudding user response:

You can try this :

string s = "PH_Value:7.539999961853027,Nitrate:1.0099999904632568,DO:10.229999542236328,Conductivity:6777.990234375,TSS:43.65999984741211";
List<double> values = new List<double>();
values = Regex.Matches(s, @"\d \.*\d*").Cast<Match>().Select(m => double.Parse(m.Value)).ToList();
  •  Tags:  
  • c#
  • Related