I have a string which contains the graph data something like this..
string sGraph = "0.05 /m 0.05 /m 0.05 /m 0.05 /m 0.05 /m 0.05 /m 0.05 /m 0.05....... 0.05 /m";
I have to extract the double value in a double[] or into a List for further processing. For ex. I use the
List<String> sGraphPoints = Regex.Split(sGraph, " /m").ToList<string>();
to retrieve the double values in a string list. I can then use each value from the string list for further processing. Is there an efficient way where i can do the same, instead of casting each string variable value into double as shown below…
double[] dGraphPoints = new double[sGraphPoints.Count];
int i = 0;
foreach (string str in sGraphPoints)
{
if (str != "")
{
dGraphPoints[i] = double.Parse(str);
}
else
{
dGraphPoints[i] = 0.0;
}
i++;
}
Many thanks in advance
This is only slightly more ‘efficient’ because it’s not using Regex and it’s not creating an extra list in between.
But really, it comes down to what are you using the values for? You might be able to modify the where clause to only select values you’re interested in, but I wouldn’t know what those would be.