0

I'm working in C# and trying to parse out the floats on each side of the "w" from the following string:

"10.3w20.5"

I want to have the floats available to use as floats in an equation. How do I accomplish this?

I tried splitting the string by length but then realized that the values on each side could be variable in length.

3
  • So, what have you tried so far? Commented Dec 21, 2011 at 7:51
  • 5
    What have you tried? looks like a split + parse should do this easily enough...? Commented Dec 21, 2011 at 7:51
  • Sorry. Yes, I forgot to mention I tried splitting the string by length but then realized that those 2 values on each side could be variable in length. Commented Dec 21, 2011 at 8:09

2 Answers 2

3

How about:

var floats = "10.3w20.5".Split('w').Select(s => Convert.ToSingle(s));
Sign up to request clarification or add additional context in comments.

2 Comments

That gives a sequence, which can be a pain to work with - as a very minor suggestion, I'd look at var floats = Array.ConvertAll("10.3w20.5".Split('w'), Convert.ToSingle); personally.
@MarcGravell: What about appending .ToArray()? Would that made a difference to your call?
3

The only thing to add to avalable answers, is what usually people forget to mantion in conversion mnagement. If you're going to operate in multiculture environment, pay attention on Culture you use to store and convert data to.

public static float ToSingle(
    string value,
    IFormatProvider provider
)

I would say, even if you're not going to operate in multiculture environment, it's always a good to pay attention on this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.