I'm not to good in Regular Expressions. I have
string rgba = "[0.123,0.223,0.2,1]";
What would be the best way to covert it into double[] ?
Yuriy Faktorovich
68.9k14 gold badges107 silver badges150 bronze badges
asked Dec 26, 2010 at 0:25
danny.lesnik
18.6k31 gold badges142 silver badges206 bronze badges
2 Answers 2
rgba.Replace("]", String.Empty)
.Replace("[", String.Empty)
.Split(',')
.Select(double.Parse)
.ToArray();
Or if you know that it will always start with [ and end with ]
rgba.Substring(1, rgba.Length - 2)
.Split(',')
.Select(double.Parse)
.ToArray();
And if you don't like LINQ
Array.ConvertAll(rgba.Substring(1, rgba.Length - 2).Split(','), double.Parse);
Regex is quite expensive to use, and I wouldn't recommend it in this case.
answered Dec 26, 2010 at 0:28
Yuriy Faktorovich
68.9k14 gold badges107 silver badges150 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You can use a regex with body:
\d+\.\d*
The regex will match one or more digits, then a single dot, then any number of digits.
answered Dec 26, 2010 at 0:57
jakobbotsch
6,3974 gold badges30 silver badges39 bronze badges
Comments
lang-cs
[0.1, 1.0, 0.5, 1 ]