Ad

How To Split A String Into Doubles And Add Them To Array C#

- 1 answer

I have a string like

"1.898, -1.456, 233.556, 34546.8"

How would I make an array of doubles in C# Do I have to use regex or split function?

I was trying something like:

string[] aux = ORIGINALtext.Split(',');
foreach (string val in aux)
{
   double value = double.Parse(val);
   Console.WriteLine(value);

}
Ad

Answer

double[] doubles = Array.ConvertAll(myDoubles.Split(','), double.Parse);

Or using LINQ

double[] doubles = myDoubles.Split(',').Select(double.Parse).ToArray();
Ad
source: stackoverflow.com
Ad