Guest User

Untitled

a guest
Aug 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. Efficient way to extract double value from a List<string> in C#
  2. 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";
  3.  
  4. List<String> sGraphPoints = Regex.Split(sGraph, " /m").ToList<string>();
  5.  
  6. double[] dGraphPoints = new double[sGraphPoints.Count];
  7. int i = 0;
  8. foreach (string str in sGraphPoints)
  9. {
  10. if (str != "")
  11. {
  12. dGraphPoints[i] = double.Parse(str);
  13. }
  14. else
  15. {
  16. dGraphPoints[i] = 0.0;
  17. }
  18.  
  19. i++;
  20. }
  21.  
  22. double[] dGraphPoints = sGraph.Split("/m")
  23. .Select(s => s.Trim())
  24. .Where(s => !string.IsNullOrEmpty(s))
  25. .Select(s => Double.Parse(s))
  26. .ToArray()
  27.  
  28. public static IList<double> Parse(string text)
  29. {
  30. List<double> list = new List<double>();
  31. if (text != null)
  32. {
  33. StringBuilder dbl = new StringBuilder(30);
  34. for (int i = 0; i < text.Length; i++)
  35. {
  36. if (text[i] == 'm')
  37. {
  38. list.Add(double.Parse(dbl.ToString(), CultureInfo.InvariantCulture));
  39. dbl.Length = 0;
  40. }
  41. else
  42. {
  43. if ((text[i] != ' ') && (text[i] != '/'))
  44. {
  45. dbl.Append(text[i]);
  46. }
  47. }
  48. }
  49. }
  50. return list;
  51. }
Add Comment
Please, Sign In to add comment