Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

How to val() in C#?

Hi,

I have string with a measurement in it e.g. "24 cm"

I used to use the val function in VB.NET to just get the 24 out of the string but there doesn't seem to be one in C#.

Short of writing one myself, is there a parsing function I could use to strip out the non numberic characters easily obviously avoing the '-' and '.' characters?

idiot
Thursday, March 18, 2004

Just "use" the Microsoft.VisualBasic namespace and set a reference to the Microsoft.VisualBasic dll, and now you can use Val. This dll is part of the core .NET framework, so there's no extra baggage for doing this in C#.

HTH,

Mark
------
Author of "Comprehensive VB .NET Debugging"

Mark Pearce
Thursday, March 18, 2004

Just to be clear, "Val" is part of the Microsoft.VisualBasic.Conversion module in the Microsoft.VisualBasic namespace.

Mark Pearce
Thursday, March 18, 2004

Try something like:

int i = int.Parse(textBox1.Text);

Tim
Thursday, March 18, 2004

Nevermind. That won't work.

Tim
Thursday, March 18, 2004

Thanks for the responses.

I will use the vb dll.

idiot
Thursday, March 18, 2004

Microsoft.VisualBasic.* is a crutch.

Use System.Text.RegularExpression.Regex to pull the numeric bit out of the string, then use System.Convert to convert it to an int.

Rick
Saturday, March 27, 2004

How about using String.LastIndexOfAny to point to the end of your number, then you can simply convert it.

Steve Jones (UK)
Friday, April 09, 2004

*  Recent Topics

*  Fog Creek Home