Efficient way to extract double value from a List<string> in C#
See the question and my original answer on StackOverflowSomething like this;
public static IList<double> Parse(string text)
{
List<double> list = new List<double>();
if (text != null)
{
StringBuilder dbl = new StringBuilder(30);
for (int i = 0; i < text.Length; i++)
{
if (text[i] == 'm')
{
list.Add(double.Parse(dbl.ToString(), CultureInfo.InvariantCulture));
dbl.Length = 0;
}
else
{
if ((text[i] != ' ') && (text[i] != '/'))
{
dbl.Append(text[i]);
}
}
}
}
return list;
}
It does not use Regex, allocates only the needed array, and a fixed size StringBuilder. And if the text was provided as a TextReader instead of a string, you could change the returned IList into an IEnumerable, use yield instead of Add, and optimize memory consumption.