1

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[] ?

1
  • assuming rgba strings have no spaces? (like [0.1, 1.0, 0.5, 1 ] Commented Dec 26, 2010 at 0:32

2 Answers 2

7
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.