user1001828
user1001828

Reputation: 47

c# - Parsing an odd string concatenation to float

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.

Upvotes: 0

Views: 312

Answers (2)

Tigran
Tigran

Reputation: 62256

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.

Upvotes: 3

Oliver
Oliver

Reputation: 45101

How about:

var floats = "10.3w20.5".Split('w').Select(s => Convert.ToSingle(s));

Upvotes: 3

Related Questions