I have this string 05201 05594 05453 05521 05330 04952 04984 04526
, and I need to turn it into an array of doubles.
How can I do so, without including the extra space at the end?
EDIT: Instead of turning into an array of doubles, I need to skip that step and add it to the end of an existing list of doubles
Try this:
var splitted = yourString.Split(new []{" "},
StringSplitOptions.RemoveEmptyEntries);
existingListOfDoubles.AddRange(splitted.Select(double.Parse));
Try it at ideone. Edited to match question
Split and convert. This assumes that your string contains only numbers.
Use a List<double>
instead of an array for more advanced functionality
string inputString = "05201 05594 05453 05521 05330 04952 04984 04526 ";
string[] results = inputString.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
List<double> d = new List<double>();
d.AddRange(results.Select(x => Convert.ToDouble(x)));
Now if you want to store in an array you just convert back the List<double>
to an array
double[] values = d.ToArray();
Instead if you want to append these results to an existing list of doubles
List<double> currentValues = new List<double>() {5345, 4213};
currentValues.AddRange(d);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With