C# equivalent to VB val() function

Although VB had it’s limitations, it was damn easy to filter an input string for a numerical value.

Here is how to do it in C#. You’ll need to use System.Text.RegularExpressions in the header

private static int VBVal(string sInput)
{
        string sOutput = string.Empty;
        MatchCollection oMatches = Regex.Matches(sInput, "\\d+");
        foreach (Match oMatch in oMatches)
        {
                sOutput += oMatch.ToString();
        }
        return (int)sOutput;
}
Posted in C# Tips

Leave a Reply

Your email address will not be published. Required fields are marked *

*