Number Parsing in Visual Basic .NET
Parsing numbers from strings is a basic but common task in many programs; here’s how to do it in Visual Basic .NET.
The System
namespace provides the number parsing functionality in Visual Basic .NET.
With Double.Parse
, we can parse floating-point numbers. Unlike Go, we don’t need to specify the bit precision as it’s determined by the Double
type.
For Integer.Parse
, we can parse integer numbers. Visual Basic .NET doesn’t require specifying the base or bit size for standard integer parsing.
To parse hexadecimal numbers, we use Convert.ToInt32
with base 16. Visual Basic .NET doesn’t automatically recognize the “0x” prefix, so we need to handle it explicitly.
UInteger.Parse
is available for parsing unsigned integers.
Integer.Parse
is used for basic base-10 integer parsing, similar to Go’s Atoi
function.
Parse functions in Visual Basic .NET throw exceptions on bad input, rather than returning an error. We use a Try-Catch block to handle these exceptions.
To run the program, save it as NumberParsing.vb
and use the Visual Basic compiler:
Next, we’ll look at another common parsing task: URLs.