Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Efficient way to extract double value from a List<string> in C#
- 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";
- List<String> sGraphPoints = Regex.Split(sGraph, " /m").ToList<string>();
- 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++;
- }
- double[] dGraphPoints = sGraph.Split("/m")
- .Select(s => s.Trim())
- .Where(s => !string.IsNullOrEmpty(s))
- .Select(s => Double.Parse(s))
- .ToArray()
- 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;
- }
Add Comment
Please, Sign In to add comment